I have a file called list.txt which looks like so:
input1
input2
input3
I am certain there is no blank line after the last line (input3). I then have a python script which will read this file line by line and write the text into some more text to create 3 files (one for each line):
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)
My problem is, this creates 4 files, not 3: script_input1.sh, script_input.sh, script_input3.sh and script_.sh. This last file has no text where the others would have input1 or input2 or input3.
It seems that Python reads my list.txt line by line, but when it reaches "input3", it somehow continues? How can I tell Python to read my file line by line, separated by "\n" but stop after the last text?