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

2024-09-30 20:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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之后,文件没有删除。它没有在本地目录中删除任何替代解决方案?在


Tags: truedatetimedatedeftlsopenconnectionconn
1条回答
网友
1楼 · 发布于 2024-09-30 20:34:36

必须先关闭文件,然后才能删除它:

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

^{pr2}$

相关问题 更多 >