0

I have this little script made with help from @metamorporpoise which moves all of my CSVs from my folder on Mac to my Google Drive main directory. I am new to google API and even python so could someone give me a hint about what needs to be done to upload all of those CSV files to specified folder in Google Drive, and not to the main directory as this script does?

import gspread
import os
gc = gspread.oauth(credentials_filename='/users/krzysztofpaszta/credentials.json')

os.chdir('/users/krzysztofpaszta/CSVtoGD')

files = os.listdir()

for filename in files:
   if filename.split(".")[1] == "csv":
      sh = gc.create(filename.split(".")[0]+' TTF')
      content = open(filename, 'r').read().encode('utf-8') 
      gc.import_csv(sh.id, content)

Thank you for the help

  • 1
    I'd check https://stackoverflow.com/questions/57606718/creating-a-spreadsheet-in-a-folder-with-gspread – blaku01 May 30 '22 at 11:40
  • 1
    If you want to achieve your goal using gspread, I think that this is the answer to your question. https://stackoverflow.com/q/71698954 So, please modify `sh = gc.create(filename.split(".")[0]+' TTF')` to `sh = gc.create(filename.split(".")[0]+' TTF', folder_id="###folderId###")`. If this modification didn't work, please update gspread and test it again. – Tanaike May 30 '22 at 12:34
  • @Tanaike thank you, I figured it out in the same moment :p – Kamilminiprogramer May 30 '22 at 12:36
  • Thank you for replying. I'm glad your issue was resolved. From your reply, I flagged as a duplicated question. – Tanaike May 30 '22 at 12:37

2 Answers2

1

I guess you want to place it in a folder in your google drive instead of at the root. By what I can see, the gc.create function creates and empty sheet, after which gc.import_csv imports data into the sheet. So you only need to change the location where the create function runs. Similar question has been asked here, and I will repeat the answer. This gist has several functions related to the gspread package, and you should take a look at the create_google_spreadsheet function, which creates the sheet under a parent folder. You need to have:

:param parent_folder_ids: A list of strings of parent folder ids (if any).

after which you can use:

body["parents"] = [
{
    'kind': 'drive#fileLink',
    'id': parent_folder_ids
}]

to set the parent folder where you want the file to be created.

M B
  • 2,700
  • 2
  • 15
  • 20
0

I just needed to add one line of code. I am learning, what can I say :p :

folder_id = '19vrbvaeDqWcxFGwPV82APWYTmBMEn-hi'
sh = gc.create(filename.split(".")[0]+' TTF', folder_id)
import gspread
import os
gc = gspread.oauth(credentials_filename='/users/krzysztofpaszta/credentials.json')

os.chdir('/users/krzysztofpaszta/CSVtoGD')

files = os.listdir()

for filename in files:
   if filename.split(".")[1] == "csv":
      folder_id = '19vrbvaeDqWcxFGwPV82APWYTmBMEn-hi'
      sh = gc.create(filename.split(".")[0]+' TTF', folder_id)
      content = open(filename, 'r').read().encode('utf-8') 
      gc.import_csv(sh.id, content)