Python:关闭和删除文件

| 我正在尝试解压缩文件,读取其中一个提取的文件,然后删除提取的文件。 提取的文件(例如,我们得到了file1和file2) 读取file1,然后将其关闭。
with open(file1, \'r\') as f:
    data = f.readline()
f.close()
使用\“数据\”进行操作。 删除提取的文件。
os.remove(file1)
一切正常,只是最后收到了这些消息。文件也被删除。如何正确关闭文件?
    /tmp/file1: No such file or directory
    140347508795048:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen(\'/tmp/file1\',\'r\')
    140347508795048:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
更新: (我的脚本看起来与此类似)
#!/usr/bin/python
import subprocess, os

infile = \"filename.enc\"
outfile = \"filename.dec\"
opensslCmd = \"openssl enc -a -d -aes-256-cbc -in %s -out %s\" % (infile, outfile)   
subprocess.Popen(opensslCmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,      close_fds=True)
os.remove(infile)
    
已邀请:
        您看到的错误不是Python报告的错误。它们的意思是Python试图打开这些文件以外的东西,尽管很难从您的小片段中分辨出什么。 如果您只是尝试从zip文件中检索某些数据,则没有真正的理由将它们提取到磁盘中。您可以直接从zip文件中直接读取数据,并使用ѭ4only仅提取到内存中。     
        将ѭ5和
file
上下文管理器一起使用时,无需关闭文件句柄,更改范围后,即完成读取行后,句柄将自动关闭。 见python教程     

要回复问题请先登录注册