-1

I'm having some issues with some python logic. Not sure if that's possible but if it's possible with bash I guess that's possible in every other language.

I want to use default function argument if no commandline argument is passed to script:

#!/usr/bin/env python3

import sys

def get_file_name(filename: str = "watched_movies"):
    if sys.argv[1]:
        filename = sys.argv[1]
        return filename
    return filename
    
print(get_file_name())

My code might be wrong because I was just doing some poking around.

I'm getting error:

Traceback (most recent call last):
  File "/home/os/scripts/python/./movies.py", line 11, in <module>
    print(get_file_name())
  File "/home/os/scripts/python/./movies.py", line 6, in get_file_name
    if sys.argv[1]:
IndexError: list index out of range
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
wilk85
  • 67
  • 10
  • `sys.argv[1]` fails *because* there are no arguments. – mkrieger1 Aug 01 '23 at 13:09
  • yes, but how can i bypass that, and if there are no sys.argv passed to script i would like to use default function argument – wilk85 Aug 01 '23 at 13:17
  • Did you read the answer to the other question I've linked? – mkrieger1 Aug 01 '23 at 13:19
  • well i have read it, but my goal is to get default name from function argument if no sys.argv provided, and i haven't found any clue in link you have gave – wilk85 Aug 01 '23 at 13:45
  • To be clear: the code worked when you *did* pass command line arguments? And you *already understood* that `sys.argv` will contain the command line arguments? If you do not pass any command line arguments, what do you think will be in `sys.argv`? How long would it be? Do you understand how to check the length of a list? Does that give you some idea about how to detect the condition and work around it? – Karl Knechtel Aug 01 '23 at 14:00
  • In a comment to the accepted answer you said "looking for len of sys.argv is the key" which is what I have been trying to tell you, and you already seem to know from your original code how you can use the default filename, just your `if` condition was wrong. – mkrieger1 Aug 01 '23 at 14:22

2 Answers2

0

Try it:

#!/usr/bin/env python3
    
import sys

def get_file_name(filename: str = "watched_movies"):
    if len(sys.argv) > 1 and sys.argv[1]:
        filename = sys.argv[1]
        return filename
    return filename 
    
print(get_file_name())

In a single line:

def get_file_name(filename: str = "watched_movies"):
    return filename if len(sys.argv) == 1 else sys.argv[1]
  • I was going to say that `sys.argv[1]` was always going to have something in it if it exists, but users can certainly pass empty arguments if they try hard enough. So good job on idiot-proofing your condition! – kindall Aug 01 '23 at 13:14
  • thank you, that was the functionality i was looking for so len of sys.argv is the key – wilk85 Aug 01 '23 at 13:48
0
#!/usr/bin/env python3

import sys

def get_file_name(filename: str = "watched_movies"):
    if len(sys.argv) < 2:
        print("no file name!")
        return
    if sys.argv[1]:
        filename = sys.argv[1]
        return filename
    return filename
    
print(get_file_name())

output

python main.py
no file name!
None

python main.py test
test

sys.argv is an array.If there is no parameter then its length is 1(just one element),index 1 is out of the range.

Wildptr
  • 15
  • 3
  • That's not the correct thing i would like to achieve, So i'm just wondering if i do not provide sys.argv, could filename: str = "watched_movies" from function can be passed as file name – wilk85 Aug 01 '23 at 13:43