Python

List Files in a Directory Python

In Python, we use the built-in “os” library to show the files in a Python directory. We use the Python os.listdir() function, which provides a complete record of all files and folders in a directory. However, the function returns a repository of every file in a whole file tree. The directory is also considered as a folder for locating and keeping files or several folders. Now, Python comes with several APIs to show the directory contents. Sometimes, while functioning with files in Python, a situation arises where you need to display the files in a directory. To fix this issue, we have come up with different example codes.

Prerequisites

Now, we are listing a few prerequisites that are essential before you move toward the examples related to this article:

  • Any operating system. In our examples, we have used Windows 10.
  • Any Python compiler. In our example, it is Spyder IDE.

Example 1

In our first illustration, we use the os.listdir() technique which accesses all files and folders in a directory. The os.listdir() method in Python displays files and folders in an indicated directory. But it does not return any special characters like “.” and “..”, which the OS uses to iterate through various directories. The function takes only one argument, i.e., the file destination of the directory whose folder and file names you want to access. Not just this, but also, it returns nothing within the subfolders explored by this approach.

We use the Spyder compiler to implement the os.listdir() method. To move further, create a new file by heading to the File menu and then start writing your Python code. Initially, we import an OS module. Then, we define a path where all our files and folders are placed. After this, we all os.listdir() function to access all the files and folders present in the specified location. We can then use a print function that displays all the accessed files and folders. The method os.listdir() is quite simple and helpful. Just two to three lines of code can fix your problem, and the code can be seen here:

import os
x = "D:\kalsoom"
dl = os.listdir(x)

print("The list of all Files and directories in this path '", x, "' :")

print(dl)

After implementing your os.listdir() function, save your code file with any name. However, don’t forget to add the “.py” extension while saving your code file. Now, it’s time to execute your program code and view the list of all files and folders on your screen. Execute by hitting “F5”:

Example 2

In our second illustration, we use the os.scandir() technique which accesses all files and folders after iterating over the whole directory. We use the Spyder compiler to implement the os.scandir() method. We use the same file to access files in the directory. The function also takes one argument just like os.listdir(). Initially, we imported an OS module just like we did earlier. Then, we define a path where all our files and folders are placed. After this, we use the os.scandir() function to access all the files and folders present in the specified location and assign it to variable “obj1”. The function first scans the whole directory. Then, it gets an index of os.DirEntry entities.

Next, we can use the print function, which shows all the files and folders situated in the specified path. After this, we use a for loop and if statement, which searches the files and folder in the specified path and then, displays the resultant file and folder name on the console screen. The method is efficient as it scans the whole directory and returns its index or iterator:

import os
p ="D:\kalsoom"
obj1 = os.scandir()
print("Files and Directories in '% s':" % p)
for entry in obj1:
     if entry.is_dir() or entry.is_file():
         print(entry.name)

Again, save your program file and press F5 to run the code:

Conclusion

Here, we discussed the two simplest methods to display the files and folders existing in the directory. The method os.listdir() will list everything present in the directory, such as all files and folders. If you want file type along with its name, you can use os.scandir(). It produces DirEntry objects which contain file type and its name.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content