在python中上传到s3后删除文件

时间:2016-06-28 05:58:05

标签: python amazon-s3

def upload(s):
     conn=tinys3.Connection("AKIAJPOZEBO47FJYS3OA","04IZL8X9wlzBB5LkLlZD5GI/",tls=True)
     f = open(s,'rb')
     z=str(datetime.datetime.now().date())
     x=z+'/'+s
     conn.upload(x,f,'crawling1')
     os.remove(s)   

我上传到s3后,文件没有删除它没有在本地目录中删除任何替代解决方案吗?

1 个答案:

答案 0 :(得分:0)

您必须先关闭该文件,然后才能将其删除:

import os

a = open('a')
os.remove('a')
>> Traceback (most recent call last):
   File "main.py", line 35, in <module>
      os.remove('a')
   PermissionError: [WinError 32] The process cannot access the file because 
   it is being used by another process: 'a'

您应该在致电f.close()之前添加os.remove,或者只使用with

with open(s,'rb') as f:
    conn = tinys3.Connection("AKIAJPOZEBO47FJYS3OA","04IZL8X9wlzBB5LkLlZD5GI/",tls=True)
    z = str(datetime.datetime.now().date())
    x = z + '/' + s
    conn.upload(x, f, 'crawling1')
os.remove(s)