Search code examples
pythonsysargs

how to check how many args given and make python take specified file if it's given?


import sys

if __name__ == '__main__':

    print(sys.argv)
    if sys.argv[1]== None:
        print('taking default file 15.wav')

    else:
        print('taking the given specified file:')
        print(sys.argv[1])

it's not working, how to check that if the file is given, the program proceses specified file?

like this:

# no file given as arg, so 
# the below will work on default 15.wav file
python program.py

python program.py 4.wav #this will work on 4.wav

Solution

  • Try it like this :

    import sys
    
    if __name__ == '__main__':
        if len (sys.argv) > 1 :
            print('taking the given specified file:', sys.argv [1])
        else :
            print('taking default file 15.wav')