在Python中关闭已打开的csv

在Python中关闭已打开的csv,python,python-3.x,csv,tkinter,Python,Python 3.x,Csv,Tkinter,Python是否有一种方法可以关闭该文件,即该文件已经打开 或者至少显示一个文件已打开的弹出窗口,或者显示一个自定义的写入错误消息弹出窗口以显示权限错误。 为了避免: PermissionError: [Errno 13] Permission denied: 'C:\\zf.csv' 我见过很多通过python打开文件然后关闭文件的解决方案。但就我而言。假设我打开了csv,然后尝试运行作业 如何使其关闭当前打开的csv 我尝试了下面的变体,但似乎没有一个能起作用,因为他们认为我在早些时候已经

Python是否有一种方法可以关闭该文件,即该文件已经打开

或者至少显示一个文件已打开的弹出窗口,或者显示一个自定义的写入错误消息弹出窗口以显示
权限错误。

为了避免:

PermissionError: [Errno 13] Permission denied: 'C:\\zf.csv'
我见过很多通过python打开文件然后关闭文件的解决方案。但就我而言。假设我打开了csv,然后尝试运行作业

如何使其关闭当前打开的csv

我尝试了下面的变体,但似乎没有一个能起作用,因为他们认为我在早些时候已经通过python打开了csv。我想我把事情复杂化了

f = 'C:\\zf.csv'
file.close()
AttributeError: 'str' object has no attribute 'close'
这会产生一个错误,因为没有对文件打开的引用,而只是字符串

甚至

theFile = open(f)
file_content = theFile.read()
# do whatever you need to do
theFile.close()
以及:

fileobj=open('C:\\zf.csv',"wb+")

if not fileobj.closed:
    print("file is already opened")
如何关闭已打开的csv

我能想到的唯一解决办法是添加messagebox,尽管我似乎无法让它检测文件

filename = "C:\\zf.csv"
if not os.access(filename, os.W_OK):
    print("Write access not permitted on %s" % filename)
    messagebox.showinfo("Title", "Close your CSV")
尝试使用上下文,该上下文将在上下文结束时平稳地管理关闭(
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
)操作:

with open(...) as theFile:
    file_content = theFile.read()

您还可以尝试将该文件复制到临时文件,然后随意打开/关闭/删除它。但是,它要求您具有对原件的读取权限

在这个例子中,我有一个文件“test.txt”,它是只写的(chmod444),如果我试图直接写入它,它会抛出一个“Permission denied”错误。我将其复制到具有“777”权限的临时文件中,以便可以对其执行我想要的操作:

import tempfile, shutil, os

def create_temporary_copy(path):
    temp_dir = tempfile.gettempdir()
    temp_path = os.path.join(temp_dir, 'temp_file_name')
    os.chmod(temp_path, 0o777);          # give full access to the tempfile so we can copy
    shutil.copy2(path, temp_path)        # copy the original into the temp one
    os.chmod(temp_path, 0o777);          # replace permissions from the original file
    return temp_path

path = "./test.txt"                      # original file
copy_path = create_temporary_copy(path)  # temp copy
with open(copy_path, "w") as g:          # can do what I want with it
    g.write("TEST\n")

您可以使用
try/except
捕获此错误并执行某些操作您无法关闭在其他程序中打开的文件,因为系统出于安全考虑对其进行控制。但问题可能是因为你们把文件放在了你们不应该使用的文件夹中——每个用户都有自己的文档文件夹。您是否试图将其保存在文件夹中或桌面上?顺便说一句:
权限被拒绝
不一定意味着该文件已打开,但您没有权限打开此文件夹中的文件。@furas对我来说,当我打开该文件时,我会在windows上获得此信息。您不能将该文件复制到一个文件夹中,以便您可以打开,随意关闭并删除它?如果在
close()
之前出现错误/异常,那么它将不会运行
close()
,也不会关闭它。在这里,您可以添加try-except方法,但在示例中未使用它。这似乎不会关闭任何内容。使用open('C:\\1\\C.csv')作为file:file\u content=theFile.read()theFile.close()不调用
theFile.close()
,它会在
块的
末尾自动完成。如果不能,它不会强制关闭它(如果在另一个程序中打开它,您无论如何也无法实现),但它不会抛出错误,您的程序将继续。
import tempfile, shutil, os

def create_temporary_copy(path):
    temp_dir = tempfile.gettempdir()
    temp_path = os.path.join(temp_dir, 'temp_file_name')
    os.chmod(temp_path, 0o777);          # give full access to the tempfile so we can copy
    shutil.copy2(path, temp_path)        # copy the original into the temp one
    os.chmod(temp_path, 0o777);          # replace permissions from the original file
    return temp_path

path = "./test.txt"                      # original file
copy_path = create_temporary_copy(path)  # temp copy
with open(copy_path, "w") as g:          # can do what I want with it
    g.write("TEST\n")