Skip to content
Advertisement

Run console command with python

This code works fine directly in CLI:

xmllint --xpath '//Entities[contains(ExpirationDate, '2015')]' app/sftp/file.xml > test.xml

Now, I need to execute the same command but in Python environment. This is what I am trying:

def process_xml(path):
    call(["xmllint --xpath '//Entities[contains(ExpirationDate, '2015')]' app/sftp/file.xml > test.xml"])

This is the error, and I am running the file and command from the same location:

OSError: [Errno 2] No such file or directory

Advertisement

Answer

you can just use os.system() if you have nothing else to do with spawned process.

but, if you really want to use subprocess.call and stream redirection, you must use it like following:

with open('myfile', 'w') as outfile:
    subprocess.call(["xmllint", "--xpath", "//Entities[contains(ExpirationDate, '2015')]", "app/sftp/file.xml"], stdout=outfile)

please, notice that subprocess.call takes program name and its params as a string list, maybe it’s the problem

Advertisement