1

I want to try to use a command line script with my python application. The task is the following, my database stores some initial data for the script and I need to execute a command line application in a following way:

$ application -parameter1 -file1

Here file1 is a file which contains my initial data, and parameter1 is an unrelated parameter.

The workflow as I see it now is the following:

initial_data = get_initial_data_from_db()
file = open('temp.txt', 'w+')
file.write(initial_data)
file.save()
os.popen4("application -parameter1 -file temp.txt")

I wonder if it's possible to execute this script (called application) without writing the file with initial data to the hard disk? E.g. is there a way send the files contents to the command directly?

5 Answers 5

1

You can use the subprocess modul

something like that:

import subprocess
bufsize =1024
initial_data = get_initial_data_from_db()
p = subprocess.Popen("application -parameter1", shell=True, bufsize=bufsize,
          stdin=subprocess.PIPE,   close_fds=True)

p.stdin.write(initial_data)
print p.communicate()

! if your application can read from stdin

Testing with Python as Application (in Eclipse) / after remark from Oleg Tarasenko :

import subprocess

initial_data = """
import sys
print sys.path
"""

for test in [1,2,3] :
    p = subprocess.Popen("C:/python26/python", shell=True, bufsize=512,
          stdin=subprocess.PIPE,  stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)

    p.stdin.write(initial_data)
    print p.communicate()

Output:

("['', 'C:\\\\dev\\\\ide\\\\eclipse\\\\plugins\\\\org.python.pydev_1.5.0.1251989166\\\\PySrc\\\\pydev_sitecustomize', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\libs', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jacob.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jiffie.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jaxen-1.1.1.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\swt.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\mysql-connector-java-3.0.17-ga-bin.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\qpslib.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\ifxjdbc.jar', 'C:\\\\server\\\\jboss\\\\client\\\\jbossall-client.jar', 'C:\\\\usr\\\\local\\\\machine', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol\\\\config', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\oknos\\\\tickcardimp\\\\bin', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\common\\\\jar\\\\shared.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\libs', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jacob.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jiffie.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jaxen-1.1.1.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\swt.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\mysql-connector-java-3.0.17-ga-bin.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\qpslib.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\ifxjdbc.jar', 'C:\\\\server\\\\jboss\\\\client\\\\jbossall-client.jar', 'C:\\\\usr\\\\local\\\\machine', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol\\\\config', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\oknos\\\\tickcardimp\\\\bin', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\common\\\\jar\\\\shared.jar', 'C:\\\\jython\\\\jython2.5.0\\\\Lib', 'C:\\\\jython\\\\jython2.5.0\\\\Lib\\\\site-packages', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\rt.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\jsse.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\jce.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\charsets.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\dnsns.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\localedata.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\sunjce_provider.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\sunpkcs11.jar', 'C:\\\\WINDOWS\\\\system32\\\\python26.zip', 'C:\\\\python26\\\\DLLs', 'C:\\\\python26\\\\lib', 'C:\\\\python26\\\\lib\\\\plat-win', 'C:\\\\python26\\\\lib\\\\lib-tk', 'C:\\\\python26']\r\n", "'import site' failed; use -v for traceback\r\n")
("['', 'C:\\\\dev\\\\ide\\\\eclipse\\\\plugins\\\\org.python.pydev_1.5.0.1251989166\\\\PySrc\\\\pydev_sitecustomize', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\libs', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jacob.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jiffie.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jaxen-1.1.1.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\swt.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\mysql-connector-java-3.0.17-ga-bin.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\qpslib.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\ifxjdbc.jar', 'C:\\\\server\\\\jboss\\\\client\\\\jbossall-client.jar', 'C:\\\\usr\\\\local\\\\machine', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol\\\\config', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\oknos\\\\tickcardimp\\\\bin', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\common\\\\jar\\\\shared.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\libs', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jacob.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jiffie.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jaxen-1.1.1.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\swt.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\mysql-connector-java-3.0.17-ga-bin.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\qpslib.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\ifxjdbc.jar', 'C:\\\\server\\\\jboss\\\\client\\\\jbossall-client.jar', 'C:\\\\usr\\\\local\\\\machine', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol\\\\config', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\oknos\\\\tickcardimp\\\\bin', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\common\\\\jar\\\\shared.jar', 'C:\\\\jython\\\\jython2.5.0\\\\Lib', 'C:\\\\jython\\\\jython2.5.0\\\\Lib\\\\site-packages', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\rt.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\jsse.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\jce.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\charsets.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\dnsns.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\localedata.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\sunjce_provider.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\sunpkcs11.jar', 'C:\\\\WINDOWS\\\\system32\\\\python26.zip', 'C:\\\\python26\\\\DLLs', 'C:\\\\python26\\\\lib', 'C:\\\\python26\\\\lib\\\\plat-win', 'C:\\\\python26\\\\lib\\\\lib-tk', 'C:\\\\python26']\r\n", "'import site' failed; use -v for traceback\r\n")
("['', 'C:\\\\dev\\\\ide\\\\eclipse\\\\plugins\\\\org.python.pydev_1.5.0.1251989166\\\\PySrc\\\\pydev_sitecustomize', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\libs', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jacob.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jiffie.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jaxen-1.1.1.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\swt.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\mysql-connector-java-3.0.17-ga-bin.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\qpslib.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\ifxjdbc.jar', 'C:\\\\server\\\\jboss\\\\client\\\\jbossall-client.jar', 'C:\\\\usr\\\\local\\\\machine', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol\\\\config', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\oknos\\\\tickcardimp\\\\bin', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\common\\\\jar\\\\shared.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\libs', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jacob.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jiffie.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\jaxen-1.1.1.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\swt.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\script_jy\\\\jars\\\\mysql-connector-java-3.0.17-ga-bin.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\qpslib.jar', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\nlibs\\\\ifxjdbc.jar', 'C:\\\\server\\\\jboss\\\\client\\\\jbossall-client.jar', 'C:\\\\usr\\\\local\\\\machine', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol\\\\config', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src\\\\build\\\\components\\\\jobcontrol', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\event\\\\src', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\oknos\\\\tickcardimp\\\\bin', 'C:\\\\dev\\\\ws\\\\central\\\\head\\\\common\\\\jar\\\\shared.jar', 'C:\\\\jython\\\\jython2.5.0\\\\Lib', 'C:\\\\jython\\\\jython2.5.0\\\\Lib\\\\site-packages', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\rt.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\jsse.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\jce.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\charsets.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\dnsns.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\localedata.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\sunjce_provider.jar', 'C:\\\\dev\\\\java\\\\jdk1.5.0_17\\\\jre\\\\lib\\\\ext\\\\sunpkcs11.jar', 'C:\\\\WINDOWS\\\\system32\\\\python26.zip', 'C:\\\\python26\\\\DLLs', 'C:\\\\python26\\\\lib', 'C:\\\\python26\\\\lib\\\\plat-win', 'C:\\\\python26\\\\lib\\\\lib-tk', 'C:\\\\python26']\r\n", "'import site' failed; use -v for traceback\r\n")
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting idea. Trying the solution. Seems I can use p.communicate() once only as after the second call I am getting <type 'exceptions.ValueError'>: I/O operation on closed file
0

This depends entirely on the features of the command line program you're calling. If it requires that you give it a filename as a parameter, you'll have to do just that. It could be, though, that if you give it a filename of "-" it will read from stdin. Some programs work that way. You'll need to read the documentation on that program to learn what options you have available to you.

Comments

0

Bryan Oakley is correct, a lot of this depends on the capabilities of the called application. But if it can't directly take stdin, there might be another way. If you're on a Unix-like system, you might be able to replace the temporary file with a named pipe. Wikipedia claims Windows has similar functionality, but I'm not familiar with it. This may require kicking off the application from another Python thread.

Comments

0

As an additional idea, you could use the built in temp file that Python has built in. I'm not extremely familiar with Python or this feature but I wanted to share this idea.

You'd still be writing a file out and executing the client program in the same fashion. But, this way, Python manages the clean up for you which might solve your question enough to not need to worry about a more complicated implementation.

Comments

0

if you are on linux, you can try passing /dev/stdin as the file which works the same as using "-" but supports programs that don't know about "-"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.