How to view current path file in Python?

In Python, you can use the os module to view the files in the current path. You can use the following code:

import os

# 获取当前路径
current_path = os.getcwd()

# 遍历当前路径下的文件
for file_name in os.listdir(current_path):
    print(file_name)

The function os.getcwd() is used to get the current path in the above code, and os.listdir() is used to get the names of all files and folders in the current path. You can then use a for loop to iterate through and print out the names of all files.

bannerAds