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