Reputation: 4385
I'm experimenting with file I/O. I have a small practice program that creates a text file when run. I packaged it with pyinstaller so that double clicking on the exe creates a new folder and places a text file with "hello world" inside of it. Easy peasy.
Then I started wondering about main()
. This is just a function like any other, right? So does that mean I can pass arguments to it at runtime?
I was thinking about the Steam client and how you can put stuff like '-dev' and '-console' in the shortcut. Is there a way to do this to a python exe that I have made?
I may be explaining terribly, so here's an example:
def makeFile(string):
if string:
f = open('mytext.txt', 'w') #create text file in local dir
print >> f, 'hello, ' + string + '! \nHow are ya?'
f.close()
else:
f = open('mytext.txt', 'w') #create text file in local dir
print >> f, 'hello, person! \nHow are ya?'
f.close()
def main(string = None):
makeFile(string)
So if I take this code and make it an exe, would I be able to add my optional arguments somehow.
I tried the above code, and the running test.exe --"myname"
but that didn't work.
Is there a way to do this?
Upvotes: 14
Views: 37313
Reputation: 323
If you run the program through command line, then sys.arg[0]
is helpful. Instead you can use
input()
Or input("<Statement to show to user>: ")
, to display a statement to the user. Create your executable (.exe) by compiling the Python script using
pyinstaller --onefile pythonscript.py
Double click on the generated .exe file, a console appears displaying the following, Statement to show to user: You can also take multiple user inputs by using multiple input()
statements in pythonscript.py. For example:
input_employeeid = input("Enter employee ID (For example: 231206): ")
input_companyname = input("Enter employer company (For example: Zampa solutions): ")
Press Enter
after every input in the console that appears.
Upvotes: 0
Reputation: 883
Just a note for completeness: there is docopt now, which makes it really easy to write even complex command line interfaces by describing it in a simple language. Documenting and parsing the interface actually becomes the same task with docopt.
Upvotes: 0
Reputation: 4674
Yes, you can do it with sys.argv
. Check out this link: http://docs.python.org/library/sys.html#sys.argv. But remember not to forget import sys
, and then you can use it.
import sys
# If there is an argument passed to your file
if len(sys.argv) > 1:
# argv[1] has your filename
filename = sys.argv[1]
print (filename)
# Output...
# new-host:~ yanwchan$ python3.2 test.py text.txt
# text.txt
argv[0]
has test.py
argv[1]
has text.txt
Edit: However, I do some more research on this topic and found out this: https://stackoverflow.com/a/4188500/1276534
As katrielalex points out, maybe you can look into argparse
as well.? It provides a lot more functionality as well as safety check. Interesting information.
And here is a great tutorial: http://www.doughellmann.com/PyMOTW/argparse/
Upvotes: 8
Reputation: 301
What you're looking for is either the sys module, or the optparse module.
sys will give you very basic control over command line args.
For example:
import sys
if __name__ == "__main__":
if len(sys.argv)>1:
print sys.argv[1]
In the above example, if you were to open up a shell and type -
test.exe "myname"
The resultant output would be:
myname
Note that sys.argv[0] is the name of the script you are currently running. Each subsequent argument is defined by a space, so in your example above
test.exe -- myname
argv[0] = "test.exe"
argv[1] = "--"
argv[2] = "myname"
Optparse gives a much more robust solution that allows you to define command line switches with multiple options and defines variables that will store the appropriate options that can be accessed at runtime.
Re-writing your example:
from optparse import OptionParser
def makeFile(options = None):
if options:
f = open('mytext.txt', 'w') #create text file in local dir
print >> f, 'hello, ' + options.name + '! \nHow are ya?'
f.close()
else:
f = open('mytext.txt', 'w') #create text file in local dir
print >> f, 'hello, person! \nHow are ya?'
f.close()
if __name__ == "__main__":
parser = OptionParser()
parser.add_option('-n','--name',dest = 'name',
help='username to be printed out')
(options,args) = parser.parse_args()
makeFile(options)
You would run your program with :
test.exe -n myname
and the output (in myfile.txt) would be the expected:
Hello, myname!
How are ya?
Hope that helps!
Upvotes: 12
Reputation: 11936
What you are looking for is something like the python argparse module
Or you can read the values directly using sys.argv
import sys
sys.argv[0] # the name of the command that was called
sys.argv[1] # the first argument, eg '--dev'
sys.argv[2] # the second...
Upvotes: 2