import os
import pandas as pd
def all_files_path(rootDir):
for root, dirs, files in os.walk(rootDir): # 分别代表根目录、文件夹、文件
for file in files: # 遍历文件
file_path = os.path.join(root, file) # 获取文件绝对路径
filepaths.append(file_path) # 将文件路径添加进列表
for dir in dirs: # 遍历目录下的子目录
dir_path = os.path.join(root, dir) # 获取子目录路径
all_files_path(dir_path) # 递归调用
if __name__ == "__main__":
dirpath = input("输入要遍历的文件夹:")
filepaths = [] # 初始化列表用来
all_files_path(dirpath)
# 记录到txt文件中
with open('dir.txt', 'a') as f:
for filepath in filepaths:
f.write(filepath + '\n')
count = 0
for filepath in filepaths:
print(filepath)
index = filepath.rfind(".")
if filepath[index:] == ".XLSX":
df = pd.read_excel(filepath)
count += df.shape[0]
if filepath[index:] == ".CSV":
df = pd.read_csv(filepath)
count += df.shape[0]
print(count)
print(count)
Python 获取文件夹下csv和xlsx文件的行数之和
最新推荐文章于 2024-09-13 19:41:31 发布