Reputation: 51
I am trying to sort my files for school automatically but when I try this:
import os, sys
path = "/Sorting for School/"
dirs = os.listdir(path)
#print all dirs
for file in dirs:
print file
Although there are two txt documents in dir but when I run this the output is:
[]
Thanks
Upvotes: 2
Views: 2779
Reputation: 36028
The code is fine. You must be querying a wrong path.
/dir/
means a subdirectory named dir
in the root directory (in UNIX) or in the root directory of the current drive (in Windows).
Upvotes: 1
Reputation: 6824
Your code is fine. The following should work too:
import os
path = "/Sorting for School/"
def handle_err(err):
print err
for root,dirs,files in os.walk(path,onerror=handle_err):
for name in files:
print(name)
Upvotes: 1