上传Google云端硬盘API - 这些额外的空白行是从哪里来的?

时间:2018-04-03 05:18:44

标签: python google-api google-drive-api google-api-python-client

总结一下程序:我从我的Google云端硬盘下载文件,然后在本地计算机上打开并读取文件(" file_a.txt"),然后我在附加模式下打开了我的机器中的另一个文件(" file_b.txt"),并且我附加了" file_a.txt"进入" file_b.txt"在使用这个新的" file_b"更新我的Google云端硬盘之前。

出于某种原因,我得到了这些空白行。它们以非常特殊的方式扩展。这是我的程序的最终输出与鬼线:

==
***
name
































class
====
***
some new song
















class
====
***
final








class
====
***
final




class
====
***
final


class
====
***
final

class
==

了解每次新的python main.py次运行时," name"之间的差距。和"班" 之前的写作从上到下变宽和变宽? 编辑:空白行的模式。他们加倍了。在我的文件中2-> 4-> 8-> 16-> 32。

这是我的" file_a.txt"我正在阅读:

==
***
name

class
==

应该只有一个空白行..

" file_b.txt"最初是空白的。

现在,这是根据Google Drive API下载和更新的python程序:

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)  

这些空白行是从哪里来的?

编辑:它与mimetype有什么关系吗? 在我的下载和上传中,它们都被定位为'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'))

修改:

有些东西告诉我换行也不是问题:

以下是我更新代码以删除换行符的方法:

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)  

在运行期间输出命令行以证明在更新之前没有流氓新行:

Download 100%.
data, ['==\n', '***\n', 'final\n', '\n', 'class\n', '==']
read []
newread []
2 ['==\n', '***\n', 'final\n', '\n', 'class\n', '==']  

并且仍在我的google驱动器上,每次更新时都会出现空白行。

0 个答案:

没有答案