2

I want to create a simple script that will upload multiple CSV to my Drive every day. Currently here is my script. The problem with this is that every time I run my program, a new CSV file will be created instead of replaced.

CLIENT_SECRET_FILE ='Client_secret_QSCtest.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive']

service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

def export_csv_file(file_path: str, parents: list=None):
    if not os.path.exists(file_path):
    print(f'{file_path} not found.')
    return
try:
    file_metadata = {
        'name' : os.path.basename(file_path).replace('.csv',''),
        'mimeType' : 'application/vnd.google-apps.spreadsheet',
        'parents' : parents
    }
       
    media = MediaFileUpload(filename=file_path, mimetype='text/csv')

    response = service.files().create(
       media_body=media,
       body=file_metadata 
    ).execute()

    print(response)
    return response

except Exception as e:
    print(e)
    return


csv_files = os.listdir('./')

for csv_files in csv_files:
   export_csv_file(os.path.join('', csv_files))
   export_csv_file(os.path.join('', csv_files), parents=['1apjzSu8fugs6EvGwJVof3MDGlhZXoT_G'])

This code creates all the CSV files in my desktop folder successfully to the Google Drive location, but how can I make it so that it overwrites the previously created file instead of creating new versions every time?

Thank you!

3
  • Why not just check if a file with the particular filename exists, and if so delete it before transferring the new file? Commented Jan 3, 2022 at 13:47
  • The problem with google drive is that you can upload the same file name, unlike windows files. This is because google drive has a unique ID for all its files instead of going by file name. Commented Jan 3, 2022 at 13:53
  • Well that's a pain. Can you search by name and get the file's ID so you can delete it? I'm not that familiar with the Drive API... Commented Jan 3, 2022 at 14:02

1 Answer 1

2

You are running a file. create

  response = service.files().create(
       media_body=media,
       body=file_metadata 
    ).execute()

If you want to update an existing file then you want to use File.update

   response = service.files().update(
    fileId=file_id,
    body=file_metadata ,
    media_body=media).execute()

What you will need to do is to do a file.list to check if the file with that name and mimetype already exists if it does then using its fileId you can update that file.

If you dont know how to search for a file check the q parameter for file.list. it will let you search for a file by name, mimetype and directory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.