Python List Files: Directory Guide

You can use the os module to list all files in a directory. Here is an example code:

import os

def list_files_in_directory(directory):
    files = os.listdir(directory)
    for file in files:
        print(file)

# 列出当前目录中的所有文件
list_files_in_directory('.')

The code above will list all files in the current directory. You can also replace ‘.’ with the path of another directory to list all files in that directory.

bannerAds