To begin, here's my code for my program:
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()
It's really novice and simple, although it does exactly what I want it to do. It takes the save files for a game I play, zips them, moves them from the program's directory where it was zipped to the desktop, and then uploads the archive to Google Drive so that I can download it to continue where I left off when playing from another computer.
I'm trying to add an additional little option to go ahead and give the option to delete the archive off of the desktop after it has been uploaded. I am running into several problems here and am stuck.
The first thing I tried was this:
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()
The problem with this is, after pressing enter to delete the archive, I get the following error thrown:
[WinError 32] The process cannot access the file because it is being used by another process
I did some reading and it seems that the file needs to be closed before being removed, but I'm stuck and am not sure how to integrate this into my code. I tried defining a variable: close_zip = r"c:/users/patjb/desktop/backup_saves.zip"
, but it tells me:
'str' object has no attribute 'close'
Any advice?