List files and folders in a directory – Python

Let’s see an easy way to list all files and folders in a directory:

import os
 
for filename in os.listdir("x:\mydir1\mydir2\"):
    print(filename)

Open command line, change to the directory of your python file and issue the following command:

python myfile.py
%d bloggers like this: