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.
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:
x = "D:\kalsoom"
dl = os.listdir(x)
print("The list of all Files and directories in this path '", x, "' :")
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”:
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:
obj1 = os.scandir()
print("Files and Directories in '% s':" % p)
for entry in obj1:
if entry.is_dir() or entry.is_file():
Again, save your program file and press F5 to run the code:
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.