Python文件名读取
作者:互联网
# -*- coding: utf-8 -*-
# @Author : LEHOSO
# @FileName: Test.py
# @Time : 2021/11/4 00:19
# coding=utf-8
import os
dir = 'D:\\xxx\\xxx'#需要读取的路径
file = os.listdir(dir)
fopen = open(r'书名.txt', 'w', encoding="utf-8")#写入到当前目录,并设置为utf-8字符
for b in file:
string = b + '\n' #每输出一个进行换行 "\n"为转义字符
line = string.replace(" ", "").replace(".pdf", "") + "\t" #拿到文件名进行数据清洗
fopen.write(line)#写入到书名.txt文本内
print(len(b)) #输出有多少文件
fopen.close()
标签:txt,utf,读取,文件名,Python,coding,replace,file,fopen 来源: https://www.cnblogs.com/lehoso/p/15550087.html