如何去读文件夹中文件的名称

xiaoxiao2021-02-28  44

方法一,window下批量处理.bat法。

1、建立一个.txt文件,输入“dir *.* /B >” 文档列表.txt,修改后缀,改为.bat。

2、双击运行,即出现一个"文档列表.txt"。里面存储的即为该文件夹下的 目录名称。

方法二:编程法

用python

import os   

def file_name(file_dir):   

for root, dirs, files in os.walk(file_dir):  

          print(root) #当前目录路径  

          print(dirs) #当前路径下所有子目录  

          print(files) #当前路径下所有非目录子文件  

importpath = "D:/Python34/news" #文件夹目录 files= os.listdir(path) #得到文件夹下的所有文件 s = [] for file in files: #遍历文件夹 if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开 f = open(path+ "/"+file); #打开文件 iter_f = iter(f); #创建迭代器 str = "" for line in iter_f: #遍历文件,一行行遍历,读取文本 str = str + line s.append(s import o path = "D:/Python34/news" #文件夹目录 files= os.listdir(path) #得到文件夹下的所有文件名称 s = [] for file in files: #遍历文件夹 if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开 f = open(path+ "/"+file); #打开文件 iter_f = iter(f); #创建迭代器 str = "" for line in iter_f: #遍历文件,一行行遍历,读取文本 str = str + line s.append(str) #每个文件的文本存到list中
转载请注明原文地址: https://www.6miu.com/read-2400148.html

最新回复(0)