小编Gui*_*Erb的帖子

Python,删除目录:错误文件存在

我需要开发一个可以启动一些计算的脚本.希望此脚本通过删除一些临时目录来正确处理^ C. 我在signal_handler中尝试了几个版本的代码:

shutil.rmtree(self.temp)
Run Code Online (Sandbox Code Playgroud)

甚至

os.system("rm -rf " + self.temp)
Run Code Online (Sandbox Code Playgroud)

当我中断执行并调用处理程序删除目录时,我收到的错误如下:

OSError: [Errno 17] File exists : 'foo' 
Run Code Online (Sandbox Code Playgroud)

要么

rm: Unable to remove directory foo: File exists
Run Code Online (Sandbox Code Playgroud)

执行后,我要删除的目录为空,我可以rm -r在shell中删除它.但是,如果我执行代码:

for f  in os.listdir(self.temp):
    os.remove(os.path.join(self.temp,f))

for f in os.listdir(self.temp):
    print f

os.rmdir(self.temp)
Run Code Online (Sandbox Code Playgroud)

我当然得到错误,但第二个循环找到这个文件: .nfsA13D3

有人解决我的问题吗?谢谢 !

python unix python-2.4

5
推荐指数
2
解决办法
2949
查看次数

标签 统计

python ×1

python-2.4 ×1

unix ×1