Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查文件是否已关闭?_Python_File Handling - Fatal编程技术网

Python 如何检查文件是否已关闭?

Python 如何检查文件是否已关闭?,python,file-handling,Python,File Handling,请帮我检查文件是否打开和关闭的代码。我尝试了下面的代码,但它不起作用。我只需要python检查chat.xls文件是否打开,如果是真的,python应该关闭chat.xls。请帮助,这就是我尝试的 closeXl = r"C:\Users\R\Downloads\Chat.xls" if not closeXl.closed: closeXl.close() AttributeError:“str”对象没有属性“closed”file\u对象。closed仅适用于由同一Python进程

请帮我检查文件是否打开和关闭的代码。我尝试了下面的代码,但它不起作用。我只需要python检查chat.xls文件是否打开,如果是真的,python应该关闭chat.xls。请帮助,这就是我尝试的

closeXl = r"C:\Users\R\Downloads\Chat.xls"
if not closeXl.closed:
    closeXl.close()

AttributeError:“str”对象没有属性“closed”

file\u对象。closed
仅适用于由同一Python进程打开的文件。在某些情况下,您应该执行
f=open(r“C:\Users\r\Downloads\Chat.xls”)
(如果您没有执行,
。closed
应该不起作用)。然后,您可以稍后检查
如果不是f.closed:
文件\u对象。closed
仅适用于由相同Python进程打开的文件。在某些情况下,您应该执行
f=open(r“C:\Users\r\Downloads\Chat.xls”)
(如果您没有执行,
。closed
应该不起作用)。然后,如果不是f.close,您可以稍后检查

您的AttributeError似乎说您应该在文件句柄上执行.close(),而不是在路径字符串上执行

closeXl=r“C:\Users\r\Downloads\Chat.xls”
文件=打开(关闭X1)
如果文件未关闭:
file.close()文件
在大多数情况下,最好使用with语句。它会在块的末尾自动关闭文件

closeXl=r“C:\Users\r\Downloads\Chat.xls”
打开(关闭X1)作为文件:
把你的密码传过来
如果要检查文件是否从另一个进程以读写方式打开并因此被锁定,应查看:

您的AttributeError似乎说您应该在文件句柄上执行.close(),而不是在路径字符串上执行

closeXl=r“C:\Users\r\Downloads\Chat.xls”
文件=打开(关闭X1)
如果文件未关闭:
file.close()文件
在大多数情况下,最好使用with语句。它会在块的末尾自动关闭文件

closeXl=r“C:\Users\r\Downloads\Chat.xls”
打开(关闭X1)作为文件:
把你的密码传过来
如果要检查文件是否从另一个进程以读写方式打开并因此被锁定,应查看:

您应该在访问
关闭的属性之前打开文件

>f=open('1.txt')
>>>f
>>>f.关闭
假的
>>>f.关闭()
>>>f.关闭
真的

您应该在访问
关闭的属性之前打开文件

>f=open('1.txt')
>>>f
>>>f.关闭
假的
>>>f.关闭()
>>>f.关闭
真的

您的AttributeError似乎说您应该在文件句柄上执行.close(),而不是在路径字符串上执行。您的AttributeError似乎说您应该在文件句柄上执行.close(),而不是在路径字符串上执行。