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)
没有找到相关结果
已邀请:
2 个回复
栖很钾是狠
稍惮
上下文管理器一起使用时,无需关闭文件句柄,更改范围后,即完成读取行后,句柄将自动关闭。 见python教程