python获取当前文件夹下所有文件名
xxxxxxxxxx
# 导入os模块
import os
# path定义要获取的文件名称的目录(C盘除外)
path = "E:\工作表格\数据统计\网站日志\日志"
# os.listdir()方法获取文件夹名字,返回数组
file_name_list = os.listdir(path)
# 转为转为字符串
file_name = str(file_name_list)
# replace替换"["、"]"、" "、"'"
file_name = file_name.replace("[", "").replace("]", "").replace("'", "").replace(",", "\n").replace(" ", "")
# 创建并打开文件list.txt
f = open(path + "\\" + "文件list.txt", "a")
# 将文件下名称写入到"文件list.txt"
f.write(file_name)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在这里插入图片描述