0

To clarify, the Python module I'm writing is a self-written .py file (named converter), not one that comes standard with the Python libraries.

Anyways, I want to somehow overload my function such that typing in

 converter file_name

will send the file's name to

 def converter(file_name):
    # do something

I've been extensively searching through Google and StackOverflow, but can't find anything that doesn't require the use of special characters like $ or command line options like -c. Does anyone know how to do this?

0

4 Answers 4

2

You can use something like PyInstaller to create a exe out of your py-file.

To use the argument in python:

import sys

if __name__ == "__main__":
    converter(sys.argv[1])
Sign up to request clarification or add additional context in comments.

4 Comments

I meant the native Windows command line, not Python's interactive interpreter.
Yes, after you created an exe, you can use it just like you said in your question.
One more thing: will it still work if I have several different functions in the file?
calling the other functions by name require either another exe or working with batch files. Have a look at spacedivers answer for another method.
1

You can type in the windows shell:

python converter.py file_name.txt

to give the arguments to the sys.argv list within python. So to access them:

import sys
sys.argv[0] # this will be converter.py
sys.argv[1] # this will be file_name.txt

at the bottom of the file you want to run, add:

if __name__ == "__main__":
    converter(sys.argv[1])

To have a second argument:

python converter.py file_name1.txt file_name2.txt

This will be the result:

import sys
sys.argv[0] # this will be converter.py
sys.argv[1] # this will be file_name1.txt
sys.argv[2] # this will be file_name2.txt

I would recommend using something like the builtin argparse (for 2.7/3.2+) or argparse on pypi (for 2.3+) if you're doing many complicated command line options.

5 Comments

what if the method converter() takes two filenames, with the second one as optional? What should be the scenario then?
Updated answer. Hopefully it helps.
Gives this error on run: Traceback (most recent call last): File "C:\Users\HP\Desktop\program.py", line 12, in <module> sys.argv[1] # this will be file_name1.txt IndexError: list index out of range
I tried the approach that you specified beforehand, but it didnt worked. Any guess?
In order to use this method, you must call python.exe first, passing in the script you would like to run. This is done with the first python in the command line arguments. Not including it does not properly send the other command arguments. Instead, use the full command: python converter.py file_name1.txt file_name2.txt
0

Only way I can think of is to create a batch file of the same name and within it, call your python script with the parameters provided.

With batch files you don't have to specify the extension (.bat) so it gets you closer to where you want to be.

Comments

0

Also, without any compiling .py to .exe, you may make your script 'executable', so that if you issue command line like myscript param param, the system will search for myscript.py and run it for you, as if it was an .exe or .bat file.

In order to achieve this, configure the machine where you plan to run your script:

  1. Make sure you have file associations set (.py to python interpreter, that is, if you doubleclick at your script in the explorer -- it gets executed). Normally this gets configured by the Python installer.
  2. Edit the COMSPEC environment variable (look inside My Computer properties) to include .PY extension as well as .EXE, .COM, etc.
  3. Start a fresh cmd.exe from Start menu to use the new value of variable. Old instances of any programs will see only old value.

This setup could be handy if you run many scripts on the same machine, and not so handy if you spread you scripts to many machines. In the latter case you better use py2exe converter to bundle up your application into a self-sufficient package (which doesn't require even python to be installed).

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.