David Jay
David Jay

Reputation: 51

os.listdir() shows nothing while it is not so

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

Answers (2)

ivan_pozdeev
ivan_pozdeev

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

ha9u63a7
ha9u63a7

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

Related Questions