| | |
Running a program with arguments
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 49
Reputation:
Solved Threads: 0
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?
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?
You can do a thing inside the program where you can append arguments. It goes something like:
python Syntax (Toggle Plain Text)
import sys sys.argv.append("ooh look an argument") #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
Check out my Site | and join us on IRC | Python Specific IRC
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: 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.
Try this example, substituting your actual command and arguments:
Python Syntax (Toggle Plain Text)
>>> p = subprocess.Popen(["dir", "/etc"], stdout=subprocess.PIPE) >>> for x in p.stdout: ... print x[:-1] # Trim tailing \n with [:-1] slice ... DIR_COLORS defaults mtab postinstall profile.d services termcap alternatives group networks preremove protocols setup bash.bashrc hosts passwd profile rpm skel >>> p.stdout.close()
•
•
Join Date: Dec 2008
Posts: 49
Reputation:
Solved Threads: 0
It's basically a program to md5 hash a password. The code is pretty simple:
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.
Python Syntax (Toggle Plain Text)
import subprocess file = open("hash.txt", 'a') code = subprocess.Popen(["md5.exe", "-da"], stdout=subprocess.PIPE) 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.
file.write(code) is wrong. Replace with: Python Syntax (Toggle Plain Text)
for line in code.stdout: # code.stdout is handle to subprocess output file.write(line)
•
•
Join Date: Nov 2007
Posts: 148
Reputation:
Solved Threads: 32
Just in case it helps, there is a built in way to get md5 hashes within python. check out the hashlib library.
python Syntax (Toggle Plain Text)
import hashlib myhash = hashlib.md5('this is a sentence') print myhash.hexdigest() myfile = open('somefile.txt','r') myhash = hashlib.md5(myfile.read()) print myhash.hexdigest()
One more thing: you probably need to add
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
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
•
•
Join Date: Dec 2008
Posts: 49
Reputation:
Solved Threads: 0
@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:
or here:
So basically directly after the writing loop, or after everything is done in the main body?
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()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()![]() |
Similar Threads
- Dynamic Array - basic program, please help (C++)
- Running a program from within a py script... (Python)
- compile program with command line arguments (C)
- Running a 3rd party program from Python (Python)
- Problem with parallel port access (*nix Hardware Configuration)
- Command-line Arguments. (C++)
- Program Compiles fine, but error upon Running (Java)
Other Threads in the Python Forum
- Previous Thread: Large File Support - Win32
- Next Thread: Classes help..
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm assignment avogadro beginner bluetooth character cmd code copy customdialog cx-freeze data decimals dictionary directory dynamic error examples excel exe file float format ftp function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop module mouse number numbers output parsing path port prime program programming projects push py2exe pygame pyglet pyqt python random recursion recursive schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable ventrilo verify vigenere webservice wikipedia windows wxpython xlib





