[Tutor] error when using using subprocess.popen in Python wrapper script
I am new to Python and am trying to figure out how to execute Linux commands via a Python wrapper. This works ** #this works okay import subprocess uname = "uname" uname_arg = "-a" subprocess.call([uname,uname_arg]) ** But this doesn't. *** #this doesn't work import subprocess #perl prog works in real life perl_prog = "perl perlprog.pl" perl_prog_h ="-h" #this is where it breaks subprocess.call([perl_prog, perl_prog_h]) I get the following error when I run the program python errorCheck.py Linux mybox.domain.com 2.6.18-238.9.1.el5 #1 SMP Tue Apr 12 18:10:13 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux Traceback (most recent call last): File "errorCheck.py", line 16, in ? subprocess.call([perl_prog, perl_prog_h]) File "/usr/lib64/python2.4/subprocess.py", line 419, in call return Popen(*args, **kwargs).wait() File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__ errread, errwrite) File "/usr/lib64/python2.4/subprocess.py", line 996, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory any suggestions? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] error when using using subprocess.popen in Python wrapper script
On Sun, May 6, 2012 at 8:23 PM, Martin A. Brown wrote: > Or, if I were in your shoes, I would do something a bit more like > this: > > cmd = [ '/usr/bin/perl', '/path/to/perlprog.pl', '-h' ] > subprocess.call(cmd) Thank you, Martin. This was helpful. Installed Strace and found out that I mispelled something. :b I suspect that I'm having a problem with it reading a long string, but I'm not sure why. (Need to escape a char? Or perhaps something is timing out?) This works cmd = ['perl','/path/to/my/script.pl, '-h'] subprocess.call(cmd) But this does not cmd = ['perl','/path/to/my/script.pl, '-x arg1 -y arg2 -z arg3'] subprocess.call(cmd) strace gives me this output *** MY COMMAND: strace -e process python myProg.py OUTPUT execve("/usr/bin/python", ["python", "myProg.py"], [/* 22 vars */]) = 0 arch_prctl(ARCH_SET_FS, 0x2b75cfb731e0) = 0 clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x2b75cfb73270) = 15883 wait4(15883, +---+ | (output from script file) ERROR: You must specify either a command to be executed or an a command file for execution [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 15883 --- SIGCHLD (Child exited) @ 0 (0) --- exit_group(0) = ? * ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] error when using using subprocess.popen in Python wrapper script
If I want to write this command to a file, would this be the right format? * import subprocess (all my variables defined okay) perl_script=subprocess.call(['perl',perl_prog,ipfile,cmd,user,timeout,]) log=open('/tmp/pythonOutput.txt',w) log.write(subprocess.call(perl_script)) * The program runs (and outputs stuff on the screen okay), but when I "cat /tmp/pythonOutput.txt", nothing is there. (While I'm not waiting for the entire program to run across all the IP addresses, I would think that something would be go out into that log file.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] IPcount = 0 when used with subprocess.call (use something to convert to integer?)
I am wrapping a Python wrapper script to "wc -l" (count lines) of a list of IP addresses *** import subprocess IPcount = subprocess.call(['wc -l file.txt | awk \'{print $1}\''], shell=True) print "You have",IPcount,"IP addresses that are alive." *** I get the following output *** 46 You have 0 IP addresses that are alive. *** Why does IPcount not equal 46? Is this what the stout is for? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IPcount = 0 when used with subprocess.call (use something to convert to integer?)
On Mon, May 7, 2012 at 8:09 PM, Rogelio wrote: > Why does IPcount not equal 46? Is this what the stout is for? FWIW, I think this seems to fix it and make IPcount an integer. import os IPcount = os.popen("wc -l file.txt | awk '{print $1}'").read() print "You have",IPcount,"IP addresses that are alive." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] using subprocess to export files in bash
While reading the subprocess documentation, I found a great example on how to call commands with a PIPE http://docs.python.org/library/subprocess.html ** output=`dmesg | grep hda` # becomes p1 = Popen(["dmesg"], stdout=PIPE) p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE) p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. output = p2.communicate()[0] How do I do this and output to a file? e.g. output = "dmesg | grep hda > log.txt' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using subprocess to export files in bash
On Tue, May 8, 2012 at 11:06 AM, David Abbott wrote: > I have used this before; > > def uptime_report(): > """Generate uptime""" > p = subprocess.Popen("uptime > /tmp/uptime.txt", > shell=True, stdout=subprocess.PIPE) > return p.stdout.readlines() > > That was from python 2.6 Looks like a lot of my problem is that my CentOS box had Python 2.4 defaulted. Looks like Popen isn't working right, even with the most simple examples. (will look at this first before I bug everyone with this problem anymore) Thanks for all your help, guys... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor