0
I’m trying to run a command line on the terminal through a python script. The command line is as follows:
vcftools --vcf <arquivo.vcf> --extract-FORMAT-info GT --out <arquivo sem extensão>
For this I have the following lines:
import os
import subprocess
import getopt
import sys
def main(argv):
inputfile_1 = ''
outputfile = ''
try:
opts, args = getopt.getopt(
argv, "hi:o:", ["ifile=", "ofile="])
except getopt.GetoptError:
print ('test.py -i <inputfile_1> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -i <inputfile_1> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile_1 = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print ('O arquivo de input i: %s' % (inputfile_1))
print ('O arquivo de output: %s' % (outputfile))
if __name__ == "__main__":
main(sys.argv[1:])
proc = subprocess.Popen(["vcftools --vcf" + inputfile_1 + "--extract-FORMAT-info GT --out" + outputfile], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
And the mistake I’m getting:
guido-5:vcfx_statistics_gvcf_herc2 debortoli$ python vcftools.py -i herc2.gvcf.statistics_checkad.vcf -o test_vcf_ad
O arquivo de input i: herc2.gvcf.statistics_checkad.vcf
O arquivo de output: test_vcf_ad
Traceback (most recent call last):
File "vcftools.py", line 30, in <module>
proc = subprocess.Popen(["vcftools --vcf" + inputfile_1 + "--extract-FORMAT-info GT --out" + outputfile], stdout=subprocess.PIPE, shell=True)
NameError: name 'inputfile_1' is not defined
Apparently the error is in the attribution of inputfile_1
at the sys.argv[1]
...
Any help would be welcome.
The tau variable input_file1 is only visible inside the main function, you must proc=... also inside this, at the end maybe
– Miguel