我遇到以下问题:进程无法访问该文件,因为它正被另一个进程使用

2024-09-20 06:29:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现我必须关闭REPORT123.txt文件,但我不确定我是否用了正确的方法

这是我的密码:

if size > 0:
os.rename("C:\\Users\\Desktop\\Folder\\log.txt", 'C:\\Users\\Desktop\\Folder\\REPORT123.txt 
path = 'C:\\Users\\Desktop\\Folder\\REPORT123.txt
files = {'file': open(path, 'rb')}
url = 'https://api.pcloud.com/uploadfile'
data = {'username': 'myemail@gmail.com', 'password': 'mypassword'}
r = requests.post(url, files=files, data=data)
print(r.text)
time.sleep(2)
open(path, 'rb').close() #I think that the problem is maybe here
time.sleep(2)
for filename in glob.glob("C:\\Users\\Desktop\\Folder\\REPORT*"):
    os.remove(filename)
f = open("log.txt", "a")
else:
print("file is not full ")

问题是:

Traceback (most recent call last):
File "C:/Users/Desktop/Folder/fileupload.py", line 48, in <module>
os.remove(filename)
PermissionError: [WinError 32] the process cannot access the file because it is being used by another process: 'C:\\Users\\Desktop\\Folder\\REPORT123.txt'

Tags: thepathtxtlogdataisosfiles