【问题标题】:deleting a file after uploading to s3 in python在python中上传到s3后删除文件
【发布时间】:2016-06-28 14:23:26
【问题描述】:
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 后没有删除它没有在本地目录中删除任何替代解决方案?

【问题讨论】:

    标签: python amazon-s3


    【解决方案1】:

    你必须先关闭文件才能删除它:

    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'
    

    您应该在调用os.remove 之前添加f.close(),或者直接使用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)
    

    【讨论】:

      猜你喜欢
      • 2012-04-06
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      相关资源
      最近更新 更多