[Tutor] python - files

Asad asad.hasan2004 at gmail.com
Sat Jan 26 03:20:52 EST 2019


Hi All ,

       I would like to know how do I make and option file as an argument on
command propmnt in python .

   At present I using :

  if len(sys.argv) == 3:
    first = sys.argv[1]
    second = sys.argv[2]
  else:

    print "enter the second argument"
It works well for the following command :
python test.py file1 file2

However I have another case where only file1 may be present so file1 is
mandatory for this script to run however file2 is optionnal :


  if len(sys.argv) == 2:
    first_log = sys.argv[1]
    second_log = sys.argv[2]

pthon test.py file1

It gives error :

   second_log = sys.argv[2]
IndexError: list index out of range


How do I acheive this because if python test.py file1 file2   then I would
process both files .

If python test.py file1  is given then I would process file1 using the
variable first_log .


Please advice .

Thanks,


More information about the Tutor mailing list