Skip to content Skip to sidebar Skip to footer

Python: Script To Make Multiple Bash Scripts

I have a file, called list.txt, which is a list of names of files: input1.txt input2.txt input3.txt I want to build a python script that will make one file for each of these filen

Solution 1:

So, answering in order:

1) Can you detail this issue? You count 4 txt files bu you got just 3 different scripts generated by your code?

2) Sure, you need to create a var, not just using the print statement 3) Just change permissions

So, to summurize, I'd use this approach:

import os
for i, file in enumerate(os.listdir("/Users/user/Desktop/Folder")):
  if"input"in file:
    with open(file) as f:
        lines = f.readlines()

        for l in lines:
            data =""
            data +="#!/bin/bash\n#BSUB -J",l+".sh","\n#BSUB -o /scratch/DBC/user/"+l+".sh.out\n#BSUB -e /scratch/DBC/user/"+l+".sh.err\n#BSUB -n 1\n#BSUB -q normal\n#BSUB -P DBCDOBZAK\n#BSUB -W 168:00\n"
            data +="cd /scratch/DBC/user\n"
            data += 'grep "input"'+l+" > result."+l+".txt"

            with open('script%s.sh'%i, 'w') as output:
                output.write(data)
            os.chmod("script%s.sh'%i", 700)

By the way, my code it's just a guess. I think you should be more explicit when stating what's your issue. I didn't understood what you wanna achieve.

Solution 2:

Another solution that, instead of creating files

script1.sh
script2.sh
script3.sh

etc. would be to create one file file script.sh

#!/bin/bash#BSUB -J "$1".sh #BSUB -o /scratch/DBC/user/"$1".sh.out#BSUB -e /scratch/DBC/user/"$1".sh.err#BSUB -n 1#BSUB -q normal#BSUB -P DBCDOBZAK#BSUB -W 168:00cd /scratch/DBC/user
grep "input""$1" > result."$1".txt

and run it using

script.sh input1
script.sh input2
script.sh input3

Post a Comment for "Python: Script To Make Multiple Bash Scripts"