Running a program with arguments

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 49
Reputation: gsingh2011 is an unknown quantity at this point 
Solved Threads: 0
gsingh2011 gsingh2011 is offline Offline
Light Poster

Running a program with arguments

 
0
  #1
Jan 10th, 2009
I'm trying to run a program using a python program. The program is supposed to run the program, get the output, and write it to a text file. The program being run (which is an exe), takes one argument, so I used:

subprocess.call(["path", "arg"])

This works fine, but the problem is when I write it to the file I get:

TypeError: argument 1 must be string or read-only character buffer, not int

I tried:

str(subprocess.call(["path", "arg"]))

Theres no error with this, but it only writes zero to the text file, not the real output of the program. Is there any way to either convert the output to a string or another command where it already is string?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 944
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345

Re: Running a program with arguments

 
0
  #2
Jan 10th, 2009
You can do a thing inside the program where you can append arguments. It goes something like:
  1. import sys
  2. sys.argv.append("ooh look an argument")
  3. #now there is an argument!
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 49
Reputation: gsingh2011 is an unknown quantity at this point 
Solved Threads: 0
gsingh2011 gsingh2011 is offline Offline
Light Poster

Re: Running a program with arguments

 
0
  #3
Jan 10th, 2009
No, I'm sorry if i wasn't clear. I'm writing a program in python but I need to call an exe with an argument in that program.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Running a program with arguments

 
1
  #4
Jan 11th, 2009
I don't know what that "it" is you are trying to write to the file. You seem to want to write the output of the program being run, but I don't think you can do that with .call() which just returns the final status of the program run, which is an integer and needs to be converted to a string to write out, but that's not what you really wanted anyway...

Try this example, substituting your actual command and arguments:
  1. >>> p = subprocess.Popen(["dir", "/etc"], stdout=subprocess.PIPE)
  2. >>> for x in p.stdout:
  3. ... print x[:-1] # Trim tailing \n with [:-1] slice
  4. ...
  5. DIR_COLORS defaults mtab postinstall profile.d services termcap
  6. alternatives group networks preremove protocols setup
  7. bash.bashrc hosts passwd profile rpm skel
  8. >>> p.stdout.close()
If this suggestion is off the mark, perhaps you could post some actual code and explain what's going wrong. Fewer ambiguous pronouns that way.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 49
Reputation: gsingh2011 is an unknown quantity at this point 
Solved Threads: 0
gsingh2011 gsingh2011 is offline Offline
Light Poster

Re: Running a program with arguments

 
0
  #5
Jan 11th, 2009
It's basically a program to md5 hash a password. The code is pretty simple:
  1. import subprocess
  2.  
  3. file = open("hash.txt", 'a')
  4. code = subprocess.Popen(["md5.exe", "-da"], stdout=subprocess.PIPE)
  5. file.write(code)

Basically, It opens the file where the final hash will be stored, then sets the variable code equal to the output of the program, then writes it to a file. md5.exe is the program I'm using which is in the same directory as the python file. -d is the command used to hash, and whatever comes after that is what will be hashed. Right now its only going to hash the letter a simply for testing purposes. The problem is that when it writes to the file I get:

Traceback (most recent call last):
File "C:\Documents and Settings\Gulshan\My Documents\Python Scripts\2md5\2md5.py", line 11, in <module>
file.write(code)
TypeError: argument 1 must be string or read-only character buffer, not Popen
0CC175B9C0F1B6A831C399E269772661

If you can't fix the problem with this much information, I would be happy to upload the md5.exe program so you can actually test it out. Just ask.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Running a program with arguments

 
0
  #6
Jan 11th, 2009
file.write(code) is wrong. Replace with:
  1. for line in code.stdout: # code.stdout is handle to subprocess output
  2. file.write(line)
This should get you closer.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 49
Reputation: gsingh2011 is an unknown quantity at this point 
Solved Threads: 0
gsingh2011 gsingh2011 is offline Offline
Light Poster

Re: Running a program with arguments

 
0
  #7
Jan 11th, 2009
Thank you! Works perfectly.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: Running a program with arguments

 
0
  #8
Jan 11th, 2009
Just in case it helps, there is a built in way to get md5 hashes within python. check out the hashlib library.
  1. import hashlib
  2.  
  3. myhash = hashlib.md5('this is a sentence')
  4. print myhash.hexdigest()
  5.  
  6. myfile = open('somefile.txt','r')
  7. myhash = hashlib.md5(myfile.read())
  8. print myhash.hexdigest()
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Running a program with arguments

 
0
  #9
Jan 12th, 2009
One more thing: you probably need to add code.stdout.close() after you are done with the subprocess output. In this case, probably right after the for loop that writes out the lines. That file was opened for you in the .Popen() call and I don't think it gets closed automatically.

Your program will work with or without the .close() but it's good form to close files when you are done with them. Otherwise (for example) you may find your program runs for a while then mysteriously stops working. Not that I'm speaking from experience or anything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 49
Reputation: gsingh2011 is an unknown quantity at this point 
Solved Threads: 0
gsingh2011 gsingh2011 is offline Offline
Light Poster

Re: Running a program with arguments

 
0
  #10
Jan 12th, 2009
@mn_kthompson:
Thanks, I'll try it out.

@BearofNH:
Thanks, but just to make sure, where would you put the close function in this code:
import subprocess

file1 = open("1 Character Database.txt", 'a')
for a in range(33,255):
        string = chr(a)
	argument = "-d" + string
	code = subprocess.Popen(["md5.exe", argument], stdout=subprocess.PIPE)
	for line in code.stdout:
                file1.write(string + " " + line)
        code.stdout.close()
or here:
import subprocess

file1 = open("1 Character Database.txt", 'a')
for a in range(33,255):
        string = chr(a)
	argument = "-d" + string
	code = subprocess.Popen(["md5.exe", argument], stdout=subprocess.PIPE)
	for line in code.stdout:
                file1.write(string + " " + line)
code.stdout.close()
So basically directly after the writing loop, or after everything is done in the main body?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC