SetContentFile() and SetContentString() methods Fail on empty files (0 Bytes)
Robert-Leland-Young opened this issue · 3 comments
Dear Sirs:
While using the Pydrive module, I noticed that the
SetContentFile() and SetContentString() methods
generate an Exception when one attempts to
create a file of 0 bytes in length.
Here is an excerpt of the code I used:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
def create_file(filename, source) :
# Create filename in Root or Subdirectory
# filename - Subdirectory Path to create on Google Drive
# source - Local file path of file to send
drive = GoogleDrive(GAUTH)
# Check for a leading '\\' in filename
if filename[0]=='\\' : filename=filename[1:] # remove leading '\'
# Check for Root Subdirectory
if '\\' not in filename : # Create filename in Root
file = drive.CreateFile({'title': filename})
file.SetContentFile(source)
file.Upload()
return
# Find lower level Subdirectory
nest=filename.split('\\')
filename=nest[-1]
nest=nest[0:-1]
# Get File ID of Parent Directory for the New file
root_list = drive.ListFile({'q': "'root' in parents and trashed=False"}).GetList()
sdid='root' # Assume no subdirectories in filename
for i in range(len(nest)) :
sdid = '' # Assume Subdirectory not found
for lid in root_list :
if lid['title'] == nest[i] : # Found Subdirectory Title
sdid = lid['id'] # Get Subdirectory ID
break
sid = [] # Assume Subdirectory not found
if sdid == '' :
print(nest[i] + ": Directory not Found")
return sid
root_list=drive.ListFile({'q': "'"+sdid+"'"+" in parents and trashed=False"}).GetList()
# Create new file in Subdirectory
file=drive.CreateFile({'title': filename, 'parents':[{'id':sdid}]})
file.SetContentFile(source)
file.Upload()
return file
def create_file_string(filename, string) :
# Create filename in Root or Subdirectory
# filename - Subdirectory Path to create
# string - string containing new file contents
#
drive = GoogleDrive(GAUTH)
# Check for a leading '\\' in filename
if filename[0]=='\\' : filename=filename[1:] # remove leading '\'
# Check for Root Subdirectory
if '\\' not in filename : # Create filename in Root
file = drive.CreateFile({'title': filename})
file.SetContentString(string)
file.Upload()
return
# Find lower level Subdirectory
nest=filename.split('\\')
filename=nest[-1]
nest=nest[0:-1]
# Get File ID of Parent Directory for the New file
root_list = drive.ListFile({'q': "'root' in parents and trashed=False"}).GetList()
sdid='root' # Assume no subdirectories in filename
for i in range(len(nest)) :
sdid = '' # Assume Subdirectory not found
for lid in root_list :
if lid['title'] == nest[i] : # Found Subdirectory Title
sdid = lid['id'] # Get Subdirectory ID
break
sid = [] # Assume Subdirectory not found
if sdid == '' :
print(nest[i] + ": Directory not Found")
return sid
root_list=drive.ListFile({'q': "'"+sdid+"'"+" in parents and trashed=False"}).GetList()
# Create new file in Subdirectory
file=drive.CreateFile({'title': filename, 'parents':[{'id':sdid}]})
file.SetContentString(string)
file.Upload()
return file
I am using Python Version 3.7.3 on Windows 7.
Again, if one tries to create a file on a Google Drive using a file of length 0 bytes, an Exception is generated and execution is terminated.
I discovered the bug using a plain ".txt" file of length 0 bytes.
Thanks Much!
Robert, I note that you started this issue with "Dear Sirs".
Doing so is unusually formal for GitHub, but that aspect is fine and indeed charming.
What is unacceptable is assuming that the contributors to this project are all men.
This is the kind of little disrespect that may be unintentional or appear trivial, but the accumulation of which makes women programmers feel unwelcome in our industry.
Please try to be inclusive in your wording.
Thanks.
Fixed in googleapis/google-api-python-client#638 (google-api-python-client >= 1.12.25) https://github.com/googleworkspace/PyDrive/blob/871f7d644dd5df1c6190f7c7eebbab9721ccd4f4/setup.py#L16