Python: Access Files in Directory
To access all files in the current directory, you can use the os module and the os.listdir() method in Python. Here is an example code:
import os
# 获取当前目录
current_dir = os.getcwd()
# 获取当前目录所有文件
files = os.listdir(current_dir)
# 打印所有文件名
for file in files:
print(file)
This code first uses the os.getcwd() method to get the current directory path, then uses the os.listdir() method to get all the file names in the current directory, and prints them out through a loop. You can further process these files according to your needs.