#! /bin/bash conda activate ch l="a b c d e f g" a=(0.005 0.01 0.05 0.1 0.2) for attack in$attacks do for i in${a[*]} do CUDA_VISIBLE_DEVICES=6 python my_analysis.py $l$i --gpu=6 done done
'''add something to a file''' defadd2file(file, insert): withopen(file, 'r') as f: content=str(f.read()) pos=content.find("---") print(pos)
withopen(file,"wt") as f: content=content[:pos]+insert+content[pos:] f.write(content)
遍历文件夹
然后编写一个函数遍历这个文件夹,同时在每个文本文件中插入
1 2 3 4 5 6 7 8
def add_iter_file(dir,insert): if not os.path.isdir(dir): s = 'xxxxx: ' + str(insert) + '%\n' add2file(dir,s) else: files= os.listdir(dir) #得到文件夹下的所有文件名称 for file in files: add_iter_file(dir+'/'+file,insert)
然后执行就可以了,比如:
1 2 3 4
atio = [0,20,40,60,80,100] for pr in ratio: x_dir = osp.join(work_dir, str(pr)) add_iter_file(x_dir, pr)
''' get the results for one file ''' defget_results(file_name): withopen(file_name, "r") as f: data = f.readlines() a = data[0][31:-1] b = data[2][13:-1] c = data[3][13:-1] d = data[4][14:-2] e = data[5][19:-2] f = data[34][36:-1]
defiter_file(dir): ifnot os.path.isdir(dir): a, b, c, d, e, f = get_results(dir) f = cr[f] df_1[a][f] = b df_2[a][f] = c df_3[a][f] = d else: files= os.listdir(dir) #得到文件夹下的所有文件名称 for file in files: iter_file(dir+'/'+file)