【发布时间】:2014-11-10 18:49:15
【问题描述】:
试图查看一个目录是否存在,如果它没有将目录移动到另一个目录,然后继续循环的下一个迭代,这就是我正在做的事情,设法让第一个循环工作,将目录移动到另一个时,仅在最后一部分正确创建所有文件,但会引发错误,错误和我的代码部分:
File "testdraft.py", line 305, in findReplace
if not os.path.exists('{}/'.format.replace+str(x)):shutil.move(str(x), '{}/'.format(replace))
AttributeError: 'builtin_function_or_method' object has no attribute 'replace'
base_direct = '{}/'.format(replace)
for x in range(1,20):
if not os.path.exists(str(x)+'/'):os.mkdir(str(x)+'/') #this part works
else: continue
shutil.copy(filename, str(x))
shutil.copy(filename1, str(x))
frag = open("fragments_procs.in", 'w')
frag.write(str(x) + "\n" + str(20-x))
shutil.copy("fragments_procs.in", str(x))
shutil.move(str(x), '{}/'.format(replace)) #believe from here and down not working
if not os.path.exists('{}/'.format(replace)+str(x)):shutil.move(str(x), '{}/'.format(replace))
else: continue
【问题讨论】:
-
更好的模型是尝试进行文件移动并在失败时采取措施。否则,您正在创建竞争条件。
-
我有一个像这样的函数“func1(replace, filename1, filename),这个函数被调用了 66 次,我试图只在那个循环中创建一个目录而不是 19,这就是为什么我创建第一个目录后继续
标签: python for-loop mkdir shutil