Python不能删除,则创建折叠

2024-06-11 20:50:07 发布

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

我正在编写一个Python脚本,检查文件夹是否存在,如果存在,则删除它,然后重写它。在

我可以创建一个目录,然后立即删除它。在

但是当我试图删除一个目录,然后创建一个目录时,我得到了以下错误:

"[Error 5] Access is denied: 'plots'"

同时:

^{pr2}$

也许斯派德在某种程度上保留了记忆?请帮忙!在

import os, shutil

dir = 'plots'
print "remove directory"
if os.path.exists(dir):
  shutil.rmtree(dir)
print os.path.exists(dir)

print "create directory"
os.mkdir(dir, 0755)
print os.path.exists(dir)

编辑:

输出首次运行(当“绘图”不存在时)

删除目录

创建目录

是的

输出第二次运行(当“绘图”确实存在时)

删除目录

创建目录

文件“C:/Python27/Spyder Workspace/JPL_impact_gnuplot/07gnuplot/02my_lammps_anal/anal”文件_lammps.py“,第60行,英寸 操作系统.mkdir(方向0755) WindowsError:[Error 5]访问被拒绝:“plots”

所以,最后: os.path.exists(dir)=错误 但访问该目录的权限被拒绝了!救命啊!在


Tags: 文件path目录绘图os错误direxists
1条回答
网友
1楼 · 发布于 2024-06-11 20:50:07

如果您的环境没有对目录的写入权限,则可能发生这种情况。在

您可以从命令行重新创建场景。在

$ touch testfile
$ ls testfile
testfile
$ chmod 555 .
$ rm testfile
$ ls testfile
ls: cannot access testfile: No such file or directory
$ touch testfile
touch: cannot touch `testfile': Permission denied

相关问题 更多 >