2
 def CreateGDriveFolder(self):

        today = str(date.today())
        print("Todays Date: " + today) 
        gauth = GoogleAuth()
        self.drive = GoogleDrive(gauth)


        folder_name = today
        folder = self.drive.CreateFile({'title' : folder_name, 'mimeType' : 'application/vnd.google-apps.folder', 'parents': [{'id': '1k1kFXJa1MTlAQrkPE0uKW-kO3Hj1Bjp5'}]})
        time.sleep(2)
        print("Folder Created")
        folder.Upload()

        #Get folder info and print to screen
        self.foldertitle = folder['title']
        self.folderid = folder['id']
        print('title: %s, id: %s' % (self.foldertitle, self.folderid))

This is my code for folder creation upon the python script being ran. The issue is, if I need to run it twice a day or something. It creates duplicate folders in my drive with the same date. I would like to just check if folder has been created. If it has, just skip and upload files to that folder or if that folder doesn't exist, create it and upload files. I've tried googling but could not find anything that suits my issue.

Thanks in advance.

Dilshan
  • 187
  • 6

2 Answers2

1

In your script, how about the following modification?

Modified script:

def CreateGDriveFolder(self):

    today = str(date.today())
    print("Todays Date: " + today)
    gauth = GoogleAuth()
    self.drive = GoogleDrive(gauth)

    parent_folder_id = '###' # Please set your parent folder ID.
    folder_name = today
    file_list = self.drive.ListFile({"q": "title='" + folder_name + "' and '" + parent_folder_id + "' in parents and " + "mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()
    if not file_list:
        folder = self.drive.CreateFile({'title' : folder_name, 'mimeType' : 'application/vnd.google-apps.folder', 'parents': [{'id': parent_folder_id}]})
        time.sleep(2)
        print("Folder Created")
        folder.Upload()

        # Get folder info and print to screen
        self.foldertitle = folder["title"]
        self.folderid = folder["id"]

    else:
        # Get folder info and print to screen
        self.foldertitle = list[0]["title"]
        self.folderid = list[0]["id"]

    print("title: %s, id: %s" % (self.foldertitle, self.folderid))
  • In this modification, first, it checks whether the folder name of folder_name is existing in the Drive. And, when the folder is not existing, your script is run. When the folder of folder_name is existing, the information of existing folder is returned.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
1

You can use the ListFile() method to check if a folder with the same name exists.


    today = str(date.today())
    print("Todays Date: " + today) 
    gauth = GoogleAuth()
    self.drive = GoogleDrive(gauth)

    folder_name = today

    # Check if folder exists
    folder = self.drive.ListFile('1k1kFXJa1MTlAQrkPE0uKW-kO3Hj1Bjp5')


    if folder['name'] == folder_name:
    # Folder exists, skip upload

else:

#Folder does not exist, create it and upload files
c0d3x27
  • 193
  • 2
  • 15