Python 批量获取文件夹/文件 名称和数量

使用方法

  • 替换 image path 为你想获取的地址

    file 获取文件名称/数量

    floder 获取文件夹名称/数量

  • 可以服务器或 CMD 中使用(不能在 Git Bash 中使用)

python getFile.py {image path} {file/floder}

文件名 getFile.py

效果如下:

代码如下:

# -*- coding:utf-8 -*-
import os
import pandas as pd
import sys

if len(sys.argv) < 3:
    print("Usage python getFile.py {image path} {file/floder}")
    sys.exit()
root_path = sys.argv[1].replace('\\', '/')
type_f = sys.argv[2]
if type_f != 'file' and type_f != 'floder':
    print("Usage {file/floder}")
    sys.exit()

File_list = []
Primary_list = []
Secondary_list = []
Count = []
Floder = []
name = root_path.split('/')[-1]
for dirpath, dirnames, files in os.walk(root_path):
    if not files:
        continue
    if dirpath == root_path:
        continue
    dir_Primary, dir_Secondary = dirpath.split('/')[-2:]
    for file in files:
        File_list.append(file)
        Primary_list.append(dir_Primary)
        Secondary_list.append(dir_Secondary)
    Floder.append(dir_Secondary)
    Count.append(str(len(files)))

if type_f == 'file':
    print(f'文件数量 {len(File_list)}')
    content_dict = {
        'PrimaryFolder': Primary_list,
        'SecondaryFolder': Secondary_list,
        'Filename': File_list,
    }
    df = pd.DataFrame(content_dict)
    df.to_csv(os.path.join(root_path, f'{name}_file.csv'), encoding='utf_8_sig')
elif type_f == 'floder':
    print(f'文件夹数量 {len(Floder)}')
    content_dict = {
        'Floder': Floder,
        'Count': Count,
    }
    df = pd.DataFrame(content_dict)
    df.to_csv(os.path.join(root_path, f'{name}_floder.csv'), encoding='utf_8_sig')
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值