这是我的计划:
import numpy as np
import scipy as sp
import itertools
import os
import csv
import shutil
src = 'src';
dst = 'dst';
data = []
n = 1
while n < 233:
charn = str(n)
for j in range(1,9): # This will represent each plug subdirectory
for i in range(1,30):
if i < 10:
chari = str(i).zfill(2)
charj = str(j).zfill(2)
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
chari = str(i)
charj = str(j).zfill(2)
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
print chari
print charj
print charn
n = n + 1
所以我将文件夹从一个目录复制到另一个目录(232个不同的文件夹),但有些文件夹有前导零。例如,它可能如下所示:
HS_BIN_I01_J01
虽然其他人看起来像这样:
HS_BIN_I12_J08(J从未超过08,而我上升到29)。
很自然地,当我编写脚本时,我需要适应这些前导零。不幸的是,当我执行它时出现以下错误:
Traceback (most recent call last):
File "cpfldr_232points.py", line 34, in <module>
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
File "/usr/lib64/python2.6/shutil.py", line 136, in copytree
names = os.listdir(src)
OSError: [Errno 2] No such file or directory: '/src/HS_BIN_I1_J01/'
BUT!当我进入目标目录时,该文件夹已正确复制为:
HS_BIN_I01_J01。
所以它说该文件夹不存在,但无论如何都要正确复制并立即停止(没有复制其他文件夹)。
这里发生了什么?
感谢您的帮助!
答案 0 :(得分:1)
您可以通过利用字符串格式化来实现代码的意图,以便在Python标准库中执行零填充和itertools模块中的product
方法:
import itertools
import shutil
import os.path
src = 'src'
dst = 'dst'
for i, j in itertools.product(range(1, 9), range(1, 30)):
dirname = 'HS_BIN_I{0:02}_J{1:02}'.format(i, j)
destdir = '{prefix}{n}'.format(prefix=dst, n=i*j)
shutil.copytree(os.path.join(src, dirname),
os.path.join(destdir, dirname))
答案 1 :(得分:0)
我认为你需要一个else语句:
if i < 10:
chari = str(i).zfill(2)
charj = str(j).zfill(2)
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
chari = str(i)
charj = str(j).zfill(2)
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
现在,您的代码将执行上述两个代码位,无论数字是否小于10。它应该是:
if i < 10:
chari = str(i).zfill(2)
charj = str(j).zfill(2)
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
else:
chari = str(i)
charj = str(j).zfill(2)
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
然而,你根本不需要第二部分。在您if i in range(1, 30):
之后:
chari = str(i).zfill(2)
charj = str(j).zfill(2)
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
如有必要,zfill会负责添加前导零。
答案 2 :(得分:0)
你的循环次数超过233次。您重复n
次232次,j
次8次,i
次29次。 232 * 8 * 29 = 53824.所以其中一件事就是你生成的路径比你想象的多得多。
其次,它完全不清楚你实际生成的路径。代码太复杂,无法轻松猜测。而不是:
shutil.copytree(src+'HS_BIN_I'+chari+'_J'+charj+'/',dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/')
考虑:
src_path = src+'HS_BIN_I'+chari+'_J'+charj+'/'
dst_path = dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/'
shutil.copytree(src_path, dst_path)
它更容易阅读,它允许您插入一个非常重要的打印语句,显示您正在发生的结果。
src_path = src+'HS_BIN_I'+chari+'_J'+charj+'/'
dst_path = dst+charn+'/'+'HS_BIN_I'+chari+'_J'+charj+'/'
print "src_path:", src_path, "dst_path:", dst_path
shutil.copytree(src_path, dst_path)
这将证明您正在生成似乎缺少密钥"/"
分隔符的路径:
src_path: 'srcHS_BIN_I10_J08/' dst_path: 'dst232/HS_BIN_I10_J08/'
更好的是,使用更高级别的格式化操作生成路径语句。您正在进行大量手动转换,format
字符串方法更干净。例如,
src_path = "{src}/HS_BIN_I{i:02d}_J{j:02d}/'.format(**locals())
对值(src
,i
和j
)进行字符串插值从本地上下文(粗略地说,您应该删除此代码的函数),并使用格式说明符,如:02d
(&#34;两位数整数,带前导零,如果需要&#34;)。
最终代码:
def copy_dirs(src, dst):
"""
Copy subfolders from `src` directory into `dst` directory,
if they are present.
"""
for n in range(1, 233):
for j in range(1, 9):
for i in range(1, 30):
src_path = "{src}/HS_BIN_I{i:02d}_J{j:02d}/".format(**locals())
dst_path = "{dst}/{n}/HS_BIN_I{i:02d}_J{j:02d}/".format(**locals())
# print "src_path:", src_path, "dst_path:", dst_path
if os.path.isdir(src_path):
shutil.copytree(src_path, dst_path)
我仍然认为你不想复制50K +目录......但也许你这样做。如果是这样,这是一种更清洁,更防弹的方式。