1

I have a simple Python Script that is designed to create a folder in the Windows directory, upload some files to Google Drive, and then move the files to the newly created folder and delete the original source file.

Everything seemed to be working as planned when I originally wrote the script, except for the fact that I wasn't actually committing the data to Google drive using the SetContentFile command from pydrive. Once I added that in, I noticed that shutil.move was copying the files, but was unable to delete the original source files. I couldn't quite figure out why, so I decided to move the shutil.move to a separate loop clause, and that seemed to fix most of my problem. Now, it is copying all of my files to the new folder as expected, but is failing to delete the most recent original source file from the root folder.

When I try to run in cmd without the pass, it tells me that the file is still open by some other process. What could be causing that and how can I fix this problem? Also, why didn't SetContentFile and shutil.move work within the same loop clause? Any help would be appreciated!

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import glob,os,shutil
import datetime, time

os.chdir(os.path.dirname(os.path.abspath(__file__)))

gauth = GoogleAuth()
#gauth.LocalWebserverAuth()

# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")

drive = GoogleDrive(gauth)

fid = '[FOLDER ID PLACEHOLDER]'

#Check to see if today's folder is created 
date = datetime.date.today()
today = date.strftime('%Y-%m-%d')

starting_folder = '[INSERT STARTING FOLDER]'

if not os.path.exists(starting_folder + "/" + today):
    os.makedirs(starting_folder + "/" + today)

destination_folder = starting_folder + "/" + today

#Change directory to the folder where FILES are stored
os.chdir(INSERT WORKING DIRECTORY)

for file in glob.glob("*.xlsx"):
    try:
        print(file)
        with open(file,"r") as f:
            fn = os.path.basename(f.name)
            fp = os.path.abspath(f.name)
            file_drive = drive.CreateFile({'parents':[{'kind':"drive#parentReference",'id':fid}],'title':fn})
            file_drive.SetContentFile(fp)
            file_drive.Upload()
            print("The file: " + fn + " has been uploaded to Google Drive.")
        #shutil.move(starting_folder + "/" + fn,destination_folder + "/" + fn)
        #    print("The file: " + fn + " has been moved to the folder.")
        f.close()
    except: 
        pass  

for file in glob.glob("*.xlsx"):
    try:
        fn = os.path.basename(file)
        shutil.move(starting_folder + "/" + fn,destination_folder + "/" + fn)
        #    print("The file: " + fn + " has been moved to the folder.")
    except:
        pass
print("All files have been uploaded to Google Drive, and the DRIVE has been updated.")
cwill_devs
  • 13
  • 4

0 Answers0