python - How to make a function accept runtime arguments -
i want define function such want accept command line arguments
below code
def tc1(resloc): uiautomator import device; 'do activity' open(resloc, 'a') text_file: text_file.write('tc1 passed \n') text_file.close() else: open(resloc, 'a') text_file: text_file.write('tc1 failed \n') text_file.close() if __name__ == '__main__': tc1("c:\\<pathtoautomation>\\results\results.txt")
now when execute same using command line python continues refer path mentioned here tc1("c:\\<pathtoautomation>\\results\results.txt")
, doesn't consider pass in runtime command line
\scripts>python.exe trail.py c:\\<pathtoautomationresults>\\results\results.txt
what looking sys.argv
the list of command line arguments passed python script. argv[0] script name (it operating system dependent whether full pathname or not). if command executed using -c command line option interpreter, argv[0] set string '-c'. if no script name passed python interpreter, argv[0] empty string.
to loop on standard input, or list of files given on command line, see fileinput module.
you use like:
import sys def main(argv): # argv. if __name__ == "__main__": main(sys.argv[1:]) # argv[0] current filename.
and call using
python yourfile.py yourargsgohere
check out more detailed use here.
Comments
Post a Comment