0

I use the following line to get list of directories of a given path in python:

os.listdir("directory")

And it returns a list of directories, but I can't figure out how to get the list of files a directory?

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105

1 Answers1

2

os.listdir() lists all names in a directory. This includes both files and subdirectories.

Take into account these names are all relative to the listed directory. Use os.path.join() to build full paths, and use os.path.isfile() and os.path.isdir() to filter the list.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343