将本地文件上传到Google云端硬盘后如何删除?

时间:2019-01-04 21:17:04

标签: python

我需要一个python函数,该函数可以将文件上传到Google云端硬盘,然后从文件系统中删除它。尝试使用以下代码时,尝试删除文件时出现错误:

def upload():
    file_metadata = {
        'name': 'thankyou.jpg',
        'mimeType': 'image/jpeg'}
    media = MediaFileUpload('thankyou.jpg')
    myfile = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    os.remove('thankyou.jpg')

上传成功完成,但是上传后我无法在本地删除文件。 我得到的错误是:

PermissionError: [WinError 32] The process cannot access the file 
because it is being used by another process: 'thankyou.jpg'

我应该怎么做才能删除文件?

2 个答案:

答案 0 :(得分:1)

我只是在Windows 10中遇到了这个问题,最后想出了以下解决方案:将用于存储MediaFileUpload对象的变量设置为None:

    media = MediaFileUpload(folder+file.name, resumable=True)
logging.info("{} - {} {}MB uploading...".format(datetime.datetime.now().strftime("%H:%M:%S"), file.name, file_size))
try:
    service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    logging.info(datetime.datetime.now().strftime("%H:%M:%S")+" - "+file.name+" uploaded")
    media = None
except Exception as err:
    logging.warning(datetime.datetime.now().strftime("%H:%M:%S")+" - "+file.name+" failed to upload:\nType: {}".format(type(err)))

try:
    os.remove(folder+file.name)
    logging.info(datetime.datetime.now().strftime("%H:%M:%S")+" - "+file.name+" deleted.")
except PermissionError as err:
    logging.error("{} - Failed to delete local file. Looks like a permision error: {}".format(datetime.datetime.now().strftime("%H:%M:%S"), err))
except Exception as err:
    logging.warning("{} - Failed to delete local file: {}".format(datetime.datetime.now().strftime("%H:%M:%S"), err))

答案 1 :(得分:0)

我已经在我的一个项目中进行了尝试,您可以添加任何内容的返回函数(我只是使用文件ID),然后在使用upload()之后,只需执行os.remove('somethingidk.jpg')

例如:

之前:

main.py

                try:
                    SendFile('output.avi', 'output.avi', 'video/avi')
                except:
                    SendFile('output.avi', 'output.avi', 'video/avi')

file-uploader.py

def SendFile(file_path, file_name, mime):
    folder_id = ''
    file_metadata = {
    'name': file_name,
    'parents': [folder_id]}
    media = MediaFileUpload(file_path,
                        mimetype=mime)
    file = service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
    print("File ID: %s" % file.get('id'))
    os.remove('output.avi')

之后:

main.py

                try:
                    SendFile('output.avi', 'output.avi', 'video/avi')
                except:
                    SendFile('output.avi', 'output.avi', 'video/avi')

                os.remove('output.avi')

file-uploader.py

def SendFile(file_path, file_name, mime):
    folder_id = ''
    file_metadata = {
    'name': file_name,
    'parents': [folder_id]}
    media = MediaFileUpload(file_path,
                        mimetype=mime)
    file = service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
    print("File ID: %s" % file.get('id'))
    return file.get('id')

编辑

或者...

只要这样做:

def SendFileIncomplete(file_path, file_name, mime):
    folder_id = ''
    file_metadata = {
    'name': file_name,
    'parents': [folder_id]}
    media = MediaFileUpload(file_path,
                        mimetype=mime)
    file = service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
    return ("File ID: %s" % file.get('id'))

def SendFile(file_path, file_name, mime):
    SendFileIncomplete(file_path, file_name, mime)
    os.remove(file_name)

这只是在函数内部使用一个函数。上传后,您将使用SendFile