0

In my script I am listing files stored in a directory using as follows:

 path ="D:/name/hello/school"
 files = os.listdir(path)

Now, when I executed it earlier it worked all fine but now it suddenly it is throwing

Window [Error 3]

Is there an alternate way to list all the files within a directory?

I have also tried os.walk(path) but it also did not work as I am getting a StopIteration error.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
kashyap
  • 31
  • 3
  • Is that all the code you are trying to run? Also we could benefit if your add the whole error trace! – go2 Apr 16 '15 at 13:07

2 Answers2

0

I believe that the answer to your question has been already discussed in these two questions:

  1. How to list all files of a directory? - they present some alternative ways of listing files in Windows using python. They've mentioned the glob module which might be helpful.
  2. Python WindowsError: [Error 3] The system cannot find the file specified when trying to rename - this one applies to the Error 3.

Good luck!

Community
  • 1
  • 1
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
0

To list all files in a directory.

from os import listdir
from os.path import isfile, join
onlyfiles = [ f for f in listdir(/path/to/directory) if isfile(join(mypath,f)) ]

To list all files and folders in a directory

os.listdir("/path/to/directory")

To find all files in a directory recursively

all_files = []
for root, dirnames, filenames in os.walk('/path/to/directory'):
  for filename in filenames:
    all_files.append(os.path.join(root, filename))
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49