python os.remove 无法访问文件
python os.remove can not access the file
为什么我得到
Traceback (most recent call last): File "C:\temp\py\tesst.py", line 8, in <module>
os.remove( PATH ) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
'C:\temp\py\test.txt'
import os
PATH = r'C:\temp\py\test.txt'
f = open ( PATH,'w')
f.write('test\n')
f.close;
os.remove( PATH )
我是不是漏掉了什么?
您正在呼叫 f.close
而不是 f.close()
。最好根据上下文打开文件,这样它会自动关闭。
import os
PATH = r'C:\temp\py\test.txt'
with open(PATH, 'wb') as f:
f.write('test\n')
os.remove(PATH)
为什么我得到
Traceback (most recent call last): File "C:\temp\py\tesst.py", line 8, in <module>
os.remove( PATH ) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
'C:\temp\py\test.txt'
import os
PATH = r'C:\temp\py\test.txt'
f = open ( PATH,'w')
f.write('test\n')
f.close;
os.remove( PATH )
我是不是漏掉了什么?
您正在呼叫 f.close
而不是 f.close()
。最好根据上下文打开文件,这样它会自动关闭。
import os
PATH = r'C:\temp\py\test.txt'
with open(PATH, 'wb') as f:
f.write('test\n')
os.remove(PATH)