I have a problem with using os.walk on Mac. If I call it from python terminal, it works perfect, but if I call it via a python script, it returns empty list. For example:
import os
path = "/Users/temp/Desktop/test/"
for _ ,_ , files in os.walk(path):
test = [my_file for my_file in files]
print test
then, it prints:
[]
and I am pretty sure that the path does exists.
Any idea what is the problem?
You most likely need to intantiate the test list outside the for loop, for this to work.
import os
path = "/Users/temp/Desktop/test/"
test = []
for _ ,_ , files in os.walk(path):
test.extend([my_file for my_file in files])
print test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With