Python 如何自动遍历文件下所有的文件,然后再对每一个文件夹读取里面的csv文件:
代码:
import os import csv # 设置要遍历的文件夹路径 folder_path = "your_folder_path" # 遍历文件夹 for root, dirs, files in os.walk(folder_path): # 遍历当前文件夹下的所有文件 for filename in files: # 判断是否为csv文件 if filename.endswith(".csv"): file_path = os.path.join(root, filename) # 读取csv文件内容 with open(file_path, 'r') as csv_file: csv_reader = csv.reader(csv_file) for row in csv_reader: print(row)
并将所有结果合并为一个dataframe输出:
import os import glob import pandas as pd # 定义要遍历的文件夹路径和输出文件路径 folder_path = "path/to/folder" output_file = "path/to/output.csv" # 定义一个空的DataFrame用于存储所有数据 all_data = pd.DataFrame() # 遍历文件夹下的所有子文件夹 for subdir in os.listdir(folder_path): subdir_path = os.path.join(folder_path, subdir) if os.path.isdir(subdir_path): # 获取子文件夹下的所有csv文件路径 csv_files = glob.glob(os.path.join(subdir_path, "*.csv")) # 遍历csv文件并添加到all_data DataFrame中 for file_path in csv_files: data = pd.read_csv(file_path) all_data = pd.concat([all_data, data]) # 将最终结果输出到一个csv文件中 all_data.to_csv(output_file, index=False)
标签:文件,遍历,Python,文件夹,file,path,csv From: https://www.cnblogs.com/ltkekeli1229/p/17633623.html