If a nonexistent filename is passed, self._fd is never initialized. The destructor still attempts to close the file descriptor. This obscures the real error (file not existing).
from __future__ importprint_functionimportpickleimportos.pathfromgoogleapiclient.discoveryimportbuildfromgoogleapiclient.httpimportMediaFileUploadfromgoogle_auth_oauthlib.flowimportInstalledAppFlowfromgoogle.auth.transport.requestsimportRequest# If modifying these scopes, delete the file token.pickle.SCOPES= ['https://www.googleapis.com/auth/drive']
defmain():
creds=None# The file token.pickle stores the user's access and refresh tokens, and is# created automatically when the authorization flow completes for the first# time.ifos.path.exists('token.pickle'):
withopen('token.pickle', 'rb') astoken:
creds=pickle.load(token)
# If there are no (valid) credentials available, let the user log in.ifnotcredsornotcreds.valid:
ifcredsandcreds.expiredandcreds.refresh_token:
creds.refresh(Request())
else:
flow=InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds=flow.run_local_server(port=0)
# Save the credentials for the next runwithopen('token.pickle', 'wb') astoken:
pickle.dump(creds, token)
service=build('drive', 'v3', credentials=creds)
file_metadata= {'name': 'foo.jpg'}
media=MediaFileUpload('files/foo.jpg',
mimetype='image/jpeg')
file=service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print ('File ID: %s'%file.get('id'))
if__name__=='__main__':
main()
Traceback (most recent call last):
File "quickstart.py", line 46, in <module>
main()
File "quickstart.py", line 39, in main
mimetype='image/jpeg')
File "/usr/local/google/home/busunkim/misc/fixit/drive-quickstart/env/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/google/home/busunkim/misc/fixit/drive-quickstart/env/lib/python3.7/site-packages/googleapiclient/http.py", line 555, in __init__
fd = open(self._filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'files/foo.jpg'
Exception ignored in: <function MediaFileUpload.__del__ at 0x7f33b398c680>
Traceback (most recent call last):
File "/usr/local/google/home/busunkim/misc/fixit/drive-quickstart/env/lib/python3.7/site-packages/googleapiclient/http.py", line 567, in __del__
if hasattr(self, _fd):
NameError: name '_fd' is not defined
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If a nonexistent filename is passed, self._fd is never initialized. The destructor still attempts to close the file descriptor. This obscures the real error (file not existing).
Closes #798
quickstart.py
Traceback (most recent call last): File "quickstart.py", line 46, in <module> main() File "quickstart.py", line 39, in main mimetype='image/jpeg') File "/usr/local/google/home/busunkim/misc/fixit/drive-quickstart/env/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) File "/usr/local/google/home/busunkim/misc/fixit/drive-quickstart/env/lib/python3.7/site-packages/googleapiclient/http.py", line 555, in __init__ fd = open(self._filename, 'rb') FileNotFoundError: [Errno 2] No such file or directory: 'files/foo.jpg' Exception ignored in: <function MediaFileUpload.__del__ at 0x7f33b398c680> Traceback (most recent call last): File "/usr/local/google/home/busunkim/misc/fixit/drive-quickstart/env/lib/python3.7/site-packages/googleapiclient/http.py", line 567, in __del__ if hasattr(self, _fd): NameError: name '_fd' is not defined