Python Forum
help with a script to determine length of an argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with a script to determine length of an argument
#6
You still need to correct the other mistakes, even with a try/except.

The sys.argv is going to return either a one element list or a string. By using sys.argv[1] you will receive an error:
Error:
Traceback (most recent call last):   File "C:/Python/Samples/scratch.py", line 3, in <module>     file_loc = sys.argv[1] IndexError: list index out of range
Remember in Python, a list starts with '0', not a '1'

So:
file_loc = sys.argv[0]
print("file_loc = ", file_loc)
print("file_loc type = ", type(file_loc))
returns:
Output:
file_loc =  C:/Python/Samples/scratch.py file_loc type =  <class 'str'> Length of 'arg' is:  28
and:

file_loc = sys.argv
print("file_loc = ", file_loc)
print("file_loc type = ", type(file_loc))
returns:

Output:
"C:\Python 3.5\python.exe" C:/Python/Samples/scratch.py file_loc =  ['C:/Python/Samples/scratch.py'] file_loc type =  <class 'list'> Length of 'arg' is:  1
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Messages In This Thread
RE: help with a script to determine length of an argument - by sparkz_alot - Nov-01-2016, 08:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,949 Mar-03-2020, 08:34 AM
Last Post: buran
  How to determine pen color from an image? robie972003 2 2,500 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,906 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 2,301 Nov-04-2018, 04:50 PM
Last Post: stullis
  how to determine if an object is a number Skaperen 6 4,119 Jul-11-2018, 08:18 PM
Last Post: Skaperen
  How Do I Determine indentation in AST? jimo 3 4,359 Jul-01-2018, 04:25 PM
Last Post: buran
  Adding a parameter/argument to a script jehoshua 11 9,625 Jan-29-2018, 09:45 AM
Last Post: jehoshua
  Determine whether a method was overridden porton 6 6,272 Nov-14-2016, 09:51 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020