如何检查给定的参数数量并使 python 获取指定文件(如果给定)?

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])

它不起作用,如何检查如果给定了文件,程序是否处理了指定的文件?

像这样:

# 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

像这样尝试:

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')