python自动化处理文件非常实用,尤其是在大量重复劳动中
本文以批量处理不同文件夹下的文本文件为例:
文件路径状态:
E:\CSDN
………….\demo1
……………………\demo1dir
……………………\demo11.txt
……………………\demo12.txt
……………………\demo13.txt
………….\demo2
……………………\demo2dir
……………………\demo21.txt
……………………\demo22.txt
……………………\demo23.txt
………….\demo3
……………………\demo3dir
……………………\demo31.txt
……………………\demo32.txt
……………………\demo33.txt
………….\demo.txt
@python
import os
PATH = r'E:\CSDN'
for elem in os.walk(PATH):
print(elem)
('E:\\CSDN', ['demo1', 'demo2', 'demo3'], ['demo.txt'])
('E:\\CSDN\\demo1', ['demo1dir'], ['demo11.txt', 'demo12.txt', 'demo13.txt'])
('E:\\CSDN\\demo1\\demo1dir', [], [])
('E:\\CSDN\\demo2', ['demo2dir'], ['demo21.txt', 'demo22.txt', 'demo23.txt'])
('E:\\CSDN\\demo2\\demo2dir', [], [])
('E:\\CSDN\\demo3', ['demo3dir'], ['demo31.txt', 'demo32.txt', 'demo33.txt'])
('E:\\CSDN\\demo3\\demo3dir', [], [])
不难发现,os.walk从指定目录(文件夹)一直往下读取
对每个目录文件(文件夹)进行返回
即:每个elem代表一个文件夹
elem[0] 第一个元素为路径名
elem[1] 第二个元素为该路径下的所有文件夹名
elem[2] 第三个元素为该路径下的所有文件(非文件夹)
@python
import os
PATH = r'E:\CSDN'
for elem in os.walk(PATH):
if len(elem[2]) > 0:
print(elem[2])
['demo.txt']
['demo11.txt', 'demo12.txt', 'demo13.txt']
['demo21.txt', 'demo22.txt', 'demo23.txt']
['demo31.txt', 'demo32.txt', 'demo33.txt']
@python
import os
PATH = r'E:\CSDN'
for elem in os.walk(PATH):
if len(elem[2]) > 0:
for f in elem[2]:
print(elem[0] + '\\' + f)
E:\CSDN\demo.txt
E:\CSDN\demo1\demo11.txt
E:\CSDN\demo1\demo12.txt
E:\CSDN\demo1\demo13.txt
E:\CSDN\demo2\demo21.txt
E:\CSDN\demo2\demo22.txt
E:\CSDN\demo2\demo23.txt
E:\CSDN\demo3\demo31.txt
E:\CSDN\demo3\demo32.txt
E:\CSDN\demo3\demo33.txt