Skip to content
Advertisement

Expanding asterisk in glob

I want to be able to print a list of all file in a directory in linux but my code only print out the first item in the directory

inside home directory is text1.txt, text2.txt, text3.txt

sys.argv[1] should be /home/* when I run it on command line:

python fileName.py /home/*

Script:

def list_file():
    listFile= glob.glob(sys.argv[1])
    return listFile

print list_file()

the output is only the first file in the directory

 ['text1.txt']

any idea? The code works fine on Windows but when I moved it to Linux, it doesn’t work anymore

Thanks

Advertisement

Answer

Since the shell expands the wildcard, you don’t need to call glob. Just print all the arguments in sys.argv

def list_file():
    return sys.argv[1:]

print list_file()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement