3

What I want to do is very simple but I can't figure out a good and not too complex solution for this. Basically I want to define some global variables that will be used for example as a folder name

global folder = "C:\\TEMP\\" + foldername

And what I want is to set the foldername value as input when running the script, something like:

python myscript.py --folder somebeautifulfoldername

so when running my script, the folder will become C:\TEMP\somebeautifulfoldername

3
  • are you asking about assigning "C:\\TEMP\\" to global variable or passing value via commandline?? Commented May 22, 2015 at 9:16
  • 1
    Something like docs.python.org/2/library/argparse.html#module-argparse Commented May 22, 2015 at 9:18
  • @teoreda : You should have actually mention this in answer, by giving and example Commented May 22, 2015 at 9:24

4 Answers 4

3

You can pass arguments to Python script like following:

python test.py arg1 arg2 arg3

And this is what you get

Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

In your case:

python myscript.py somebeautifulfoldername

folder = "C:\\TEMP\\" + sys.argv[1]

Sign up to request clarification or add additional context in comments.

1 Comment

How are these arguments in the "argument list" accessed in the Python script?
3

You can use the built-in argparse module for this combined with getting the command line arguments from sys.argv:

import argparse
import sys

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description = 'My script')
    parser.add_argument('--folder', help = "Subfolder of C:\TEMP\ to manipulate")
    args = parser.parse_args(sys.argv[1:])
    folder = "C:\\TEMP\\"+args.folder
    print folder

Here I added just a very simple argument with some basic help string, but you can do quite a lot with this like giving a default value, allowing a list of files instead of a single file, specify the type, ... . See the manual for more details and examples.

Usage:

>python myscript.py --folder somebeautifulfoldername
C:\TEMP\somebeautifulfoldername

>python myscript.py --help
usage: tmp.py [-h] [--folder FOLDER]

My script

optional arguments:
  -h, --help       show this help message and exit
  --folder FOLDER  Subfolder of C:\TEMP\ to manipulate

Comments

1
import sys

folder = "none"
if("--folder" in  sys.argv):
    folder = sys.argv[sys.argv.index("--folder") + 1]        
print folder

If you run it the way you want:

python myscript.py --folder "HELLOFOLDER"

It will give: HELLOFOLDER

Comments

0

There are a number of options to parse command line arguments into a Python script. There's the standard library's optparse and argparse for instance.

A really nice 3rd party tool is docopt, which allows you to write the logic described above very easily by describing the script usage directly as documentation of your script like this:

"""My script

Usage:
  myscript.py --folder=<folder>

Options:
  -h --help             Show this screen.
  --version             Show version.
  --folder=<folder>     Choose folder.
"""

from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__, version='myscript 1.0')
    folder = "C:\\TEMP\\" + arguments["--folder"]
    print(folder)

That said, you may also want to look into tempfile for generating temporary files to make the script more cross-platform. Hard-coding Windows-specific paths is rarely a good idea.

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.