0

It appears that somewhere in the process of updating or downloading files from Google Docs using their API, Google adds one new line for every one of your blank lines. See: Google Drive API on upload--where are these extra blank lines coming from?

I'm trying to figure out how to get rid of the new lines created by Google while keeping my own intentionally placed new lines.

I'm finding it difficult to target when exactly it's occurring, in order to strip the \r or \n.

I thought it may occur during download, but I can't seem to get this file to print anything:

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())
        # mylist = f.read()

        # final = [line.rstrip('\n') for line in f]  

When I'm writing to the file that will eventually be uploaded on the Drive, print show's no indication that there are any rogue new lines. Things look the way they should be:

    with open("file_b.txt", "a+") as f:
        readfile = f.readlines()
        # readfile = [line.rstrip('\n') for line in f]
        # readfile = readfile.splitlines()
        print("read", readfile)
        # readfile.remove('\n')
        list2 = [a for a in readfile if a != '\n']
        print("newread", list2)
        # for line in readfile:
        #     print(line)
        #     line.replace('\n', '')
        # print("replaced", readfile)
        # readfile.
        # mylist = [line.rstrip('\n') for line in myfile]
        # print(mylist)
        list2.extend(data)

        print("2", list2)
        for item in list2:
            f.write(item)  

nothing seems to work.

How do I strip these ghost lines from Google Drive?

Jay Jung
  • 1,805
  • 3
  • 23
  • 46
  • Try `with open("file_b.txt", "a+", newline='') as f:`. Are you on Windows? – roganjosh Apr 03 '18 at 08:28
  • @roganjosh macOS – Jay Jung Apr 03 '18 at 08:31
  • Also, there's a lot of talk about GoogleDrive but the only local verification seems to be via `print`. Have you opened the file locally after it's written, and prior to upload, to be sure it's to do with GD? The newline characters will be added by the writer which is why I suggested the `newline=''` argument - has that changed anything? – roganjosh Apr 03 '18 at 08:34
  • I have the files open while the program is running. They begin in the proper state. They change directly after the program is finished uploading. If it's not with GD, I'm not sure what else it can be. – Jay Jung Apr 03 '18 at 08:41
  • But you have a local copy of the file too, after you've written the extra lines? Have you opened that? My guess is that there's a mismatch in MacOS carriage returns and those used on Google Drive, but you should also verify that a local copy is ok. I can't research this properly right now. – roganjosh Apr 03 '18 at 08:41
  • Correct. The way the program is written, and what GD requires is for the download function--`export_media`--to be provided a `filepath` that will contain the contents of the file from GD. So that `filepath` eventually reflects exactly what is on my GD docs. And what is on my docs is dictated by what I update. I am updating file_b.txt. _After upload_, file_b.txt changes to include more blank lines than it had prior to the upload. – Jay Jung Apr 03 '18 at 08:48
  • And with the `newline=''` argument, the result is unchanged? – roganjosh Apr 03 '18 at 08:58
  • @roganjosh it doesn't. If you see my recent edit here: https://stackoverflow.com/questions/49622725/google-drive-api-on-upload-where-are-these-extra-blank-lines-coming-from/ you'll see that removing newlines locally doesnt seem to prevent the google doc from showing blanks. – Jay Jung Apr 03 '18 at 09:00
  • Please stop opening new questions asking the same thing stick with one and improve it. stackoverflow.com/q/49622725/1841839 stackoverflow.com/q/49625386/1841839 stackoverflow.com/q/49627579/1841839 – Linda Lawton - DaImTo Apr 03 '18 at 10:27

0 Answers0