# -*- coding: utf-8 -*-
"""
Created on Mon May 27 13:05:46 2024
@author: Administrator
"""
from datetime import datetime
import os,os.path,sys
def get_filename(root_path):
n = 0
for root, dirs, files in os.walk(root_path, topdown = True):
for name in files:
rootpath = root.split("\\")
all_path = os.path.join(root,name)
if len(rootpath) > 2:
print(rootpath[-2],rootpath[-1],name)
content = rootpath[-2] + ',' + rootpath[-1] + ',' + name + ',' + all_path + '\n'
else:
print(root,name)
content = root + ',' + name + ',' + all_path + '\n'
n = n + 1
write_list(content)
print(n)
def write_list(content):
#current_path = os.path.dirname(os.path.abspath(__file__))
current_path = os.path.dirname(sys.argv[0])
filename = os.path.join(current_path,start_time + '.csv')
with open(filename, 'a', encoding='GB18030') as file:
file.write(content)
if __name__ == '__main__':
root_path = r'你的路径'
root_path = os.path.abspath(root_path)
start_time = datetime.now().strftime("%Y%m%d%H%M%S")
if os.path.isdir(root_path):
get_filename(root_path)
给定一个路径统计文件写到csv文件里面,并呈现上级目录和上上级目录名和文件名以及全路径,打印文件数量不含文件夹,编码18030