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()

猜你喜欢

转载自blog.csdn.net/m0_52426915/article/details/121170602