subprocess - Python sub process call -
what trying accomplish in few words this: change directories , call script shell.
so far have managed change directories os.chdir()
.
however haven't been able understand how syntax second part of given task. specifically, command want invoke 1 /path-to-dir-of-the-script/script<inputfile.txt>outfile.txt
, eyes @ least problem input file (and evidently output file not exist generated script) in 2 different directories.
so trying following (ls
, print
debugging , supervising purposes more or less) along various modifications getting error. either syntaxerror or system cannot find 2 files, etc.
import subprocess import os import sys subprocess.call(["ls"]) #read contents of current dir print os.dir('/path-to-dir') subprocess.call(["ls"]) print in_file = open(infile.txt) #i not sure if declaring files necessity. out_file = open (outfile.txt) com = /path-to-dir-of-the-script/script process = subprocess.call([com], stdin=infile.txt, stdout=outfile.txt)
this last implementation of generates: nameerror: name
infileis not defined
i sure there more 1 errors in approach (except form syntax) , have study more. far ve taken in doc includes popen
examples , 2 or 3 pertinent questions here , here , here .
in case didn't made myself clear notes :
script , files not on same level. command valid , works flawless when comes down it. moving either files, either script same level won't work.
any suggestions??
use quotes create string in python e.g.:
com = "/path-to-dir-of-the-script/script"
you use cwd
argument run script different working directory e.g.:
subprocess.check_call(["ls"]) # read contents of current dir subprocess.check_call(["ls"], cwd="/path-to-dir")
to emulate bash command:
$ /path-to-dir-of-the-script/script < inputfile.txt > outfile.txt
using subprocess
module:
import subprocess open("inputfile.txt", "rb") infile, open("outfile.txt", "wb") outfile: subprocess.check_call(["/path-to-dir-of-the-script/script"], stdin=infile, stdout=outfile)
Comments
Post a Comment