首先,以下是我的程序代码:
import os, shutil
import keyboard
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
print("Zipping MedianXL SP save files to an archive...")
shutil.make_archive("backup_saves", "zip", r"c:/users/x/appdata/roaming/medianxl/save")
print("Done! Now doing some housekeeping and uploading the archive to Google Drive...")
source_path = r"c:/users/x/onedrive/documents/programming/python/medianxlbackup/backup_saves.zip"
destination_path = r"c:/users/x/desktop"
shutil.move(source_path, destination_path)
auth = GoogleAuth()
auth.LocalWebserverAuth()
drive = GoogleDrive(auth)
saves = drive.CreateFile()
saves.SetContentFile(r"c:/users/x/desktop/backup_saves.zip")
saves.Upload()
它确实是新手和简单的,尽管它确实做了我想要它做的事情。它为我玩的一个游戏保存文件,将它们拉入拉链,将它们从压缩到桌面的程序目录中移动,然后将存档上传到Google Drive,这样我就可以从另一台计算机上下载它,继续玩我停止玩的游戏
我正在尝试添加一个额外的小选项来继续,并提供在上传档案后从桌面上删除档案的选项。我在这里遇到了几个问题,我被卡住了
我尝试的第一件事是:
while True:
if keyboard.is_pressed("enter"):
os.remove(r"c:/users/x/desktop/backup_saves.zip")
print("All files cleaned up, happy farming!")
break
else:
if keyboard.is_pressed("esc"):
exit()
问题是,在按enter键删除存档文件后,会引发以下错误:
[WinError 32] The process cannot access the file because it is being used by another process
我做了一些阅读,似乎需要关闭文件才能删除,但我卡住了,不知道如何将其集成到我的代码中。我试图定义一个变量:close_zip = r"c:/users/patjb/desktop/backup_saves.zip"
,但它告诉我:
'str' object has no attribute 'close'
有什么建议吗
据我所知,要关闭任何文件,都会使用“close()”方法。因此,要关闭zip文件,必须键入调用它的变量,并遵循我的close方法
例如,变量_包含_file.close()
相关问题 更多 >
编程相关推荐