Here's my code for a very simple program:
import os, shutil
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
source_path = r"c:/users/x/appdata/roaming/medianxl/save"
destination_path = r"c:/users/x/desktop/backup_saves"
print("Contents being backed up:")
print(os.listdir(source_path))
destination = shutil.copytree(source_path, destination_path)
print("Contents successfully backed up to:", destination_path)
print("Now uploading backup saves to Google Drive...")
auth = GoogleAuth()
auth.LocalWebserverAuth()
drive = GoogleDrive(auth)
saves = drive.CreateFile()
saves.SetContentFile(r"c:/users/x/desktop/backup_saves")
saves.Upload()
So far I am having no issues when it comes to taking the folder from the appdata location and copying it to my desktop. Where I am getting the error in my title is when I go to upload that folder and its contents on my desktop to Google Drive using pydrive.
Here's the output from the command window after running the program:
Contents being backed up:
['preferences.json', 'TSW', 'uhp_prettycolor.d2s', 'uhp_prettycolor.key', 'uhp_prettycolor.ma0', 'uhp_prettycolor.map']
Contents successfully backed up to: c:/users/x/desktop/backup_saves
Now uploading backup saves to Google Drive...
Your browser has been opened to visit:
url here
Authentication successful.
Traceback (most recent call last):
File "backup.py", line 21, in <module>
saves.SetContentFile(r"c:/users/x/desktop/backup_saves")
File "C:\Users\x\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydrive\files.py", line 169, in SetContentFile
self.content = open(filename, 'rb')
PermissionError: [Errno 13] Permission denied: 'c:/users/x/desktop/backup_saves'
I've tried running cmd as admin but am still getting the same permissions error. Any ideas?
U need to check ur backup file is opening or not. if opening, close it.
u can try it to move to another disk (D: or E:) to test first.
Related
i want to upload a local folder to google drive with python
Folder example
1 folder level
C:\Users\test\Documents\google drvie\test\
the folder you want to upload
Folder name: upload
C:\Users\test\Documents\google drvie\test\upload
* In the upload folder
There is still a hierarchy of folders and files.
I want to upload all folders and files in upload.
C:\Users\test\Documents\google drvie\test\upload\upload2
C:\Users\test\Documents\google drvie\test\upload\test.txt
C:\Users\test\Documents\google drvie\test\upload\upload2\uplpad3
C:\Users\test\Documents\google drvie\test\upload\upload2\uplpad3\test.txt
I did it with the script below, but I can't upload by folder.
Only files can be uploaded.
When uploading by folder, the following access permission error is displayed.
If anyone knows, I would appreciate it if you could tell me.
error contents
Traceback
GoogleDriveFile({'parents': [{'id': '1J8TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}], '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: [Errno 13] Permission denied: 'C:\\Users\\test\\Documents\\google drvie\\test\\upload'
Reference page
How to Upload File to Google Drive using Python Script?
code
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
import os
#Authenticate Google services
gauth = GoogleAuth()
# load credentials or create empty credentials if none exist
gauth.LoadCredentialsFile("mycreds.txt")
#If you don't have Google service credentials
if gauth.credentials is None:
#Automatically receive authorization code from user and configure local web server
gauth. LocalWebserverAuth()
# if the access token does not exist or has expired
elif gauth.access_token_expired:
#refresh authorization for google services
gauth. Refresh()
# if none match
else:
#Authorize Google services
gauth. Authorize()
# save credentials to file in txt format
gauth.SaveCredentialsFile("mycreds.txt")
#Authentication process for Google Drive
drive = GoogleDrive(gauth)
# specify folder path to upload
path = r'C:\Users\test\Documents\google drvie\test'
# File ID to upload to GOOGLE DRIVE
folder_id='1J8TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
Loop processing (repeated processing) by #for statement
for x in os.listdir(path):
# Create GoogleDriveFile object
#f = drive.CreateFile({'title' : x})
f = drive.CreateFile({"parents": [{"id": folder_id},]})
#file title
f['title'] = x
# set local file and upload
print(f)
f.SetContentFile(os.path.join(path,x))
print(f)
#Upload to Google Drive
f.Upload()
print(f)
f = None
Sorry for the inconvenience, but thank you in advance.
I'm using Flask to make a web application and I want to upload a user input file to Google Storage Cloud. I'm using Heroku to host my web app and I don't know how to save files on Heroku's temporary storage so I'm trying to use tempfile to store the file in a directory and then access the directory to upload the file.
When I try to do that, I get this error: PermissionError: [Errno 13] Permission denied: 'C:\\Users\\[MyName]\\AppData\\Local\\Temp\\tmpbpom7ull'
Here is my code I'm working with, if anyone has any other way to upload a FileStorage object to the Google Storage cloud or a way to access the saved file, that would be very appreciated!
# File is currently a "FileStorage" object from werkzeug, gotten by doing
# file = request.files["filename"]
tempdir = tempfile.mkdtemp()
file.name = filename
file.save(tempdir)
upload_blob(BUCKET_NAME,filename,filename)
Following up on yesterday's Flask: Could not authenticate question the Google Cloud Storage client, you can use werkzeug's FileStorage object as described in the Flask-GoogleStorage usage:
Assuming you a have a file hellofreddie.txt in the working directory:
hellofreddie.txt:
Hello Freddie!
You can then open it, create a FileStorage object and then use the save on Bucket object (files):
from datetime import timedelta
from flask import Flask
from flask_googlestorage import GoogleStorage, Bucket
from werkzeug.datastructures import FileStorage
import os
files = Bucket("files")
storage = GoogleStorage(files)
app = Flask(__name__)
app.config.update(
GOOGLE_STORAGE_LOCAL_DEST = app.instance_path,
GOOGLE_STORAGE_SIGNATURE = {"expiration": timedelta(minutes=5)},
GOOGLE_STORAGE_FILES_BUCKET = os.getenv("BUCKET")
)
storage.init_app(app)
with app.app_context():
with open("hellofreddie.txt","rb") as f:
file = FileStorage(f)
filename = files.save(file)
After the code has run, you will see a UUID-named equivalent created in Cloud Storage.
You can use the storage browser or gsutil:
gsutil ls gs://${BUCKET}
gs://{BUCKET}/361ea9ea-5599-4ff2-84d1-3fe1a802ac08.txt
NOTE I was unable to resolve an issue trying to print either files.url(filename) or files.signed_url(filename). These methods correctly return the Cloud Storage Object but as PurePosixPath('f3745268-5c95-4c61-a892-09c0de556635.txt'). My Python naivete.
I've realized my error, I was trying to use file.save() to a folder and not to an actual file, my code has been updated to
tempdir = tempfile.mkdtemp()
file.name = filename
file.save(tempdir + "/" + filename)
upload_blob(BUCKET_NAME,tempdir + "/" + filename,filename)
Thank you to PermissionError: [Errno 13] Permission denied
Could someone please give me some guidance. I am pretty fresh with python.
All I am wanting to do is download zip files from web addresses and save them to a folder.
The same process happens every month so I am trying to automate it.
I have sucessfully download the zip file to a folder of my choice, but when I get python to create the folder I get permission errors. I have read online I can use os.chmod and grant permissions but I cannot figure out how to structure it/ write it so it works
This is what I have so far.
import requests, zipfile, io
import os
from datetime import datetime
d = datetime.today().strftime('%b%y') #'Dec20'
newpath = 'L:/Support/Data_load/NativeTitle/{}'.format(d)
if not os.path.exists(newpath):
os.makedirs(newpath)
os.chmod(newpath , 0o0777)
#chmod -R 777 'L:/Support/Data_load/NativeTitle/{}'.format(d)
print('Folder created in L:\Support\Data_load\NativeTitle')
print('Beginning file download with urllib2...')
url = 'http://www.nntt.gov.au/GeoDocs//ESRI/NTDA_Schedule_Nat_shp.zip'
urllib.urlretrieve(url, newpath)
IOError: [Errno 13] Permission denied: 'L:/Support/Data_load/NativeTitle/Oct21'
When I run my python script for downloading an image from Firebase storage I get the following error "PermissionError: [Errno 13] Permission denied:". I have tried running as administrator without success and also checked the permissions and it appears in my settings I have full read and write access so I'm not sure what to do?
This is the code I am running:
import os
from google.cloud import storage
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'C:/Users/username/Documents/NNFILES/credential_file.json'
storage_client = storage.Client()
bucket = storage_client.get_bucket('project.appspot.com')
imageBlob = bucket.blob("/")
imagePath1 = "C:\\Users\\username\\Downloads"
Blob = bucket.blob('result_5.png')
Blob.download_to_filename(imagePath1)
Blob.download_as_string()
Also to add I am successfully uploading to Firebase storage so I do not think it is an issue with the .json file or Firebase.
imagePath1 = "C:\\Users\\username\\Downloads" is not a filename. You're trying to copy a file over the top of a directory. You need to add the file name to the end of that string.
I am new to Google Drive API and writing a simplest form of a script that automatically upload an image from the local drive on to google drive, then once that image is uploaded, delete the local copy, following is what I have got:
#%%
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from googleapiclient.http import MediaFileUpload
g_login = GoogleAuth()
g_login.LocalWebserverAuth()
drive = GoogleDrive(g_login)
#%%
header = 'images/dice'
path = header + str(i) + '.png'
file = drive.CreateFile()
file.SetContentFile(path)
file.Upload()
if file.uploaded:
print("test")
os.remove(path)
however when attempting in deleting the local copy, following error occurs:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'images/dice1.png'
I searched it up, thinking it might be the SetContentFile(path) where it did no close the file after Upload(), which according to
https://gsuitedevs.github.io/PyDrive/docs/build/html/pydrive.html
it should close automatically after upload.
What am I overseeing here?
Note: In the end, I want to use a loop that go through all the files within the directory.
This is the output:
1
test
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
<ipython-input-21-2aeb578b5851> in <module>
9 if file.uploaded:
10 print("test")
---> 11 os.remove(path)
12
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'images/dice1.png'
Even if PyDrive does not close it for you, from looking into the code, it looks like you can do something like this:
...
try:
file.Upload()
finally:
file.content.close()
if file.uploaded:
...
could you give a try please and see if that helps?