How do I accept user input on command line for python script instead of prompt -


i have python code asks user input. (e.g. src = input('enter path src: '). when run code through command prompt (e.g. python test.py) prompt appears 'enter path src:'. want type in 1 line (e.g. python test.py c:\users\desktop\test.py). changes should make? in advance

replace src = input('enter path src: ') with:

import sys src = sys.argv[1] 

ref: http://docs.python.org/2/library/sys.html

if needs more complex admit, use argument-parsing library optparse (deprecated since 2.7), argparse (new in 2.7 , 3.2) or getopt.

ref: command line arguments in python


here example of using argparse required source , destination parameters:

#! /usr/bin/python import argparse import shutil  parser = argparse.argumentparser(description="copy file") parser.add_argument('src', metavar="source", help="source filename") parser.add_argument('dst', metavar="destination", help="destination filename") args = parser.parse_args()  shutil.copyfile(args.src, args.dst) 

run program -h see message.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -