我有一个名为LIST.TXT文件看起来像这样分离:读取文件的 n,而是忽略最后 n
input1
input2
input3
我敢肯定有最后一行后无空行(输入3 )。然后,我有一个Python脚本将由线读取这个文件线和文字写入一些文字创建3个文件(每行一个):
import os
os.chdir("/Users/user/Desktop/Folder")
with open('list.txt','r') as f:
lines = f.read().split('\n')
#for l in lines:
header = "#!/bin/bash \n#BSUB -J %s.sh \n#BSUB -o /scratch/DBC/user/%s.sh.out \n#BSUB -e /scratch/DBC/user/%s.sh.err \n#BSUB -n 1 \n#BSUB -q normal \n#BSUB -P DBCDOBZAK \n#BSUB -W 168:00\n"%(l,l,l)
script = "cd /scratch/DBC/user\n"
script2 = 'grep "input" %s > result.%s.txt\n'%(l,l)
all= "\n".join([header,script,script2])
with open('script_{}.sh'.format(l), 'w') as output:
output.write(all)
我的问题是,这将创建4个文件,不3:script_input1.sh,script_input.sh,script_input3.sh和script_.sh。最后一个文件没有文本,其他文件将具有input1或input2或input3。
似乎Python逐行读取我的list.txt,但是当它到达“input3”时,它以某种方式继续?我该如何让Python逐行读取我的文件,用“\ n”分隔,但在最后一个文本之后停止?
的[从文件中读取列表中删除换行符(可能的复制https://stackoverflow.com/questions/4319236/remove-the-newline-character-in-a-list-read -from-a-file) – Mort
我会再说一次[https://stackoverflow.com/questions/46685755/python-script-to-make-multiple-bash-scripts#comment80321657_46685755]:你可能应该重新思考你的方法。 – tripleee