find the contents of a directory in python

To find the contents of a directory in Python, you can use the os module. The os.listdir() function returns a list of all the files and directories in the specified path.

Here's an example code snippet:

main.py
import os

path = "/path/to/directory"
directory_contents = os.listdir(path)

for item in directory_contents:
    print(item)
126 chars
8 lines

Replace /path/to/directory with the actual path of the directory whose contents you want to list.

The os.listdir() function returns the contents of a directory as a list of strings. It includes the names of all files, directories, and subdirectories.

related categories

gistlibby LogSnag