list all the files in the current directory and all subdirectories and print the full path to the fil in python

We can use the os module in Python to iterate over all the files in a directory (current directory, in this case) and its subdirectories. We can use the os.walk() function to walk through the directory tree, and for each file, we can print its full path using the os.path.join() function as shown below:

main.py
import os

# Set the directory you want to start from
root_dir = '.'

for dirpath, dirnames, filenames in os.walk(root_dir):
    for file in filenames:
        # Get the full path to the file
        file_path = os.path.join(dirpath, file)
        print(file_path)
265 chars
11 lines

In the code above, we first set the root_dir variable to the current directory (i.e., .). Then, we use the os.walk() function to traverse through all the directories and subdirectories of the root directory. For each file, we use the os.path.join() function to concatenate the directory path and file name, and then print the resulting full path.

gistlibby LogSnag