-3

I want to define a function such that I want it to accept command line arguments

below is my code

def tc1(resloc):
    from uiautomator import Device;
    'Do some activity'
    with open(resloc, 'a') as text_file:
            text_file.write('TC1 PASSED \n')
            text_file.close()
    else:
        with open(resloc, 'a') as text_file:
            text_file.write('TC1 FAILED \n')
            text_file.close()    

if __name__ == '__main__':
    tc1("C:\\<pathtoautomation>\\Results\results.txt")

Now when I execute the same using command line from python it continues to refer to the path mentioned here tc1("C:\\<pathtoautomation>\\Results\results.txt") and doesn't consider what I pass in the runtime from the command line

\Scripts>python.exe trail.py C:\\<pathtoautomationresults>\\Results\results.txt
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Simmi
  • 1
  • 5
  • 5
    Did you take a look at [`sys.argv`](https://docs.python.org/2/library/sys.html#sys.argv)? – bereal Aug 05 '15 at 11:29
  • 3
    possible duplicate of [Command Line Arguments In Python](http://stackoverflow.com/questions/1009860/command-line-arguments-in-python) – tripleee Aug 05 '15 at 11:30
  • You want `tc1(sys.argv[1])` or possibly a loop over `sys.argv[1:]`. – tripleee Aug 05 '15 at 11:31

2 Answers2

0

What you are looking is sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.

To loop over the standard input, or the list of files given on the command line, see the fileinput module.

You use it like:

import sys

def main(argv):
    # do something with argv.


if __name__ == "__main__":
   main(sys.argv[1:])  # the argv[0] is the current filename.

and call it using

python yourfile.py yourargsgohere

Check out a more detailed use here.

f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
0

You need to use sys.argv to get command line parameters.

in your code it could look like this:

import sys
...  # other stuff

if __name__ == '__main__':
    tc1(sys.argv[1])

There are many tutorials out there that help you use sys.argv

b3000
  • 1,547
  • 1
  • 15
  • 27