问题
To summarize the program: I'm downloading a file from my Google Drive, I'm then opening and reading a file("file_a.txt") in my local machine, then I'm opening another file in my machine("file_b.txt") in append mode, and I'm appending "file_a.txt" into "file_b.txt" before updating my Google Drive with this new "file_b".
For some reason I'm getting these blank lines. And they expand in a very peculiar way. Here's the final output of my program with the ghost lines:
==
***
name
class
====
***
some new song
class
====
***
final
class
====
***
final
class
====
***
final
class
====
***
final
class
==
See how with every new successive python main.py
run, the gap between "name" and "class" of the previous writes gets wider and wider, top to bottom?
edit: there's a pattern to the blank lines. They are doubling. 2->4->8->16->32 in my files.
Here's my "file_a.txt" that I'm reading from:
==
***
name
class
==
There should only be one blank line..
"file_b.txt" is initially blank.
Now here's the python program that downloads and updates according to the Google Drive API:
drive_service = discovery.build('drive', 'v3', http=http)
file_id, name = search_file(drive_service, 10, "name contains 'test_file'")
print(file_id, name)
if verified_file(a, b):
file_path = "file_b.txt"
downloaded = download_file(drive_service, file_id, file_path)
with open('file_a.txt', 'r') as myfile:
data = myfile.read()
with open("file_b.txt", "a+") as myfile:
myfile.seek(0, 2)
myfile.write(data)
path_of_new_file = "file_b.txt" # the file to upload
file_name_on_drive = "test_file" # the name to upload as
if file_id:
update_file(drive_service, file_id, path_of_new_file, file_name_on_drive)
Where are these blank lines coming from?
edit: does it have anything to do with the mimetype?
In both my download, and upload, they are being targeted as 'text/plain'
:
def download_file(service, file_id, filepath):
request = service.files().export_media(fileId=file_id, mimeType='text/plain')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
with io.open(filepath,'wb') as f:
fh.seek(0)
f.write(fh.read())
def update_file(service, file_id, path_of_new_file, file_name_on_drive):
mimetype = 'text/plain'
file_metadata = {'name': file_name_on_drive}
media = MediaFileUpload(path_of_new_file,
mimetype=mimetype)
file = service.files().update(fileId=file_id,
body=file_metadata,
media_body=media).execute()
print('File ID: %s' % file.get('id'))
Edit:
Something tells me newlines aren't the issue either:
Here's how I updated the code to strip newlines:
with open('file_a.txt', 'r') as myfile:
data = myfile.readlines()
print("data,", data)
with open("file_b.txt", "a+", newline='') as f:
readfile = f.readlines()
print("read", readfile)
list2 = [a for a in readfile if a != '\n']
print("newread", list2)
list2.extend(data)
print("2", list2)
for item in list2:
f.write(item)
path_of_new_file = "file_b.txt" # the file to upload
file_name_on_drive = "test_file" # the name to upload as
if file_id:
update_file(drive_service, file_id, path_of_new_file, file_name_on_drive)
And Command line output during run to prove there arent rogue newlines right before updating:
Download 100%.
data, ['==\n', '***\n', 'final\n', '\n', 'class\n', '==']
read []
newread []
2 ['==\n', '***\n', 'final\n', '\n', 'class\n', '==']
and still on my google drive, there will be blank lines that get wider with every update.
来源:https://stackoverflow.com/questions/49622725/google-drive-api-on-upload-where-are-these-extra-blank-lines-coming-from