I would like to upload a folder that is local to python to Google Drive.
I would like to upload the folder that I created locally to Google drive.
folder hierarchy
Folder 1 Tier
C:\Users\test\Documents\google drvie\test\
Folder you want to upload
folder name:upload
C:\Users\test\Documents\Google drvie\test\upload
u In the upload folder,
Folder file hierarchy still exists.
I would like to upload all the folder files in the upload.
C:\Users\test\Documents\Google drvie\test\upload\test.txt
C:\Users\test\Documents\Google drvie\test\upload\upload2
C:\Users\test\Documents\Google drvie\test\upload\upload2\test.txt
C:\Users\test\Documents\Google drvie\test\upload\upload2\uppad3
C:\Users\test\Documents\Google drvie\test\upload\upload2\uppad3\test.txt
I did it in the script below, but I can't upload each folder.
You can upload only files.
If you upload by folder, the following access permission error will be displayed.
If anyone understands, I would appreciate it if you could let me know.
Is there another way to upload automatically?
Error Contents
GoogleDriveFile({'parents':[{'id':'1J8TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}], 'title': 'upload'})
Traceback (most recent call last):
File "c:\Users\test\Documents\google drvie\googledrive_file_up.py", line 43, in <module>
f.SetContentFile(os.path.join(path,x))
File "C:\Users\test\AppData\Roaming\Python\Python39\site-packages\pydrive\files.py", line 169, in SetContentFile
self.content=open(filename, 'rb')
PermissionError: [Errno13] Permission denied: 'C:\\Users\\test\\Documents\\Google drvie\\test\\upload'
Reference Page
Create, move files, and bulk Google Drive folders in Python, PyDrive
Upload files to Google Drive in Python
Code
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
importos
#Authenticate Google Services
gauth=GoogleAuth()
# Load credentials or create empty credentials if none exists
gauth.LoadCredentialsFile("mycreds.txt")
# If you do not have Google Service credentials
if gauth.credentials is None:
# Automatically receive authentication codes from users and set up local web servers
gauth.LocalWebserverAuth()
# If the access token does not exist or has expired
elifgauth.access_token_expired:
# Refresh Google Services Authentication
gauth.Refresh()
# If neither match
else:
# Approve Google Services
gauth.Authorize()
# Save credentials to a file in txt format
gauth.SaveCredentialsFile("mycreds.txt")
# Google Drive Authentication Processing
drive=GoogleDrive(gauth)
# Specify folder path to upload
path=r'C:\Users\test\Documents\Google drvie\test'
# File ID to be uploaded to GOOGLE DRIVE
folder_id='1J8TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# Loop processing with for statement (repeat processing)
for x inos.listdir (path):
# Create GoogleDriveFile Object
#f=drive.CreateFile({'title':x})
f=drive.CreateFile({"parents":[{"id":folder_id},]})
# File Title
f['title'] = x
# Set and upload local files
print(f)
f.SetContentFile(os.path.join(path,x))
print(f)
# Upload to Google Drive
f.Upload()
print(f)
f = None
I apologize for the inconvenience, but I appreciate your cooperation.
python python3 pydrive
Perhaps the Google Drive API does not have the ability to "upload folders."
Therefore, if you want to upload a folder (including multiple tiers), you will need to do the following on your own:
As it will be long, I will refrain from quoting, but for example, the contents of the following article may be helpful.
reference:
Upload folder to Google Drive using pyDrive
Archive multiple files in advance
(either zip or tar is acceptable)
From "File" on the left side of colab
Deployment is done (either) with colab cells prepared as follows
!unzipZIPFILE.zip-d/content/drive/MyDrive/
!tar xf TARFILE.tar-C/content/drive/MyDrive/
The following sample is appropriate. (Mount authentication cannot be automated)
from google.colab import drive
drive.mount('/content/drive')
for_in range(10):
# Retrieving Files from an HTTP server
!wget...
# deployment
!unzip
# Or use zipfile module to deploy
time.sleep (3600)
© 2024 OneMinuteCode. All rights reserved.