0
我有两个几乎相同的代码。唯一的区别是,一个代码使用argparse,以便它可以在命令行运行,如下所示:Python for循环未在一个脚本中运行,但在另一个脚本中运行
parser = argparse.ArgumentParser()
parser.add_argument('directory', help = 'Main Directory of Files')
parser.add_argument('chip_num', help = 'Chip Number')
args = parser.parse_args()
path = args.directory
chip_num = args.chip_num
的其他代码简单地路径和chip_num定义为我写入脚本变量。
我有以下代码遍历目录中的子文件夹(编辑长度,但给主图片)。
for root, dirs, files in os.walk(path):
for d in dirs:
if d.startswith('pass') or d.startswith('fail'):
new_txt = 'Chip%s%s.txt' % (chip_num, d)
path_new = os.path.join(results_dir, new_txt)
tot_qscore = 0
tot_reads = 0
with open(path_new, 'w') as myfile:
myfile.write('File Name \t')
## writes other titles
for rootfolder, blankdirs, fast5files in os.walk(d):
for filename in fast5files:
if filename.endswith('.fast5'):
filepath = os.path.join(rootfolder, filename)
with h5py.File(filepath, 'r') as hdf:
with open(path_new, 'a') as myfile:
myfile.write('%s \t' % (filename))
## gets other variables and prints it to the file
tot_qscore += qscore
tot_reads += 1
avg_qscore = float(tot_qscore/tot_reads)
虽然代码运行完全在我写的变量,可以用命令行中使用的脚本的脚本能够运行脚本,但不知何故绕过for循环,通过穿越“ d'目录(对于rootfolder,blankdirs,os.walk(d)中的fast5files),并直接进入计算avg_qscore,因此给我错误,因为tot_reads没有增加,所以我除以零。
是否有任何理由跳过for循环...是否受到argparse的影响?
你应该在'path'中包含'tree'的输出。 – robert
对不起,你的意思是树的输出? – j2120
http://linux.die.net/man/1/tree – robert