python打印中文路径_Python 打印指定目录下指定格式的文件列表

先直接贴代码

import sys

import os

def print_file_name(file_dir, file_format = "None"):

if (file_format == "None"):

for root, dirs, files in os.walk(file_dir):

for ele in files:

print(ele)

break

else:

for root, dirs, files in os.walk(file_dir):

for ele in files:

temp_format = os.path.splitext(ele)[1]

if (temp_format.lstrip(".") == file_format.strip().lstrip(".")):

print(ele)

break

params_ = sys.argv

lens_ = len(params_)

if lens_ > 3:

print("Error: too many params.")

else:

if (lens_ == 1):

abs_path = os.path.split(os.path.abspath(__file__))

print_file_name(abs_path[0])

sys.exit()

elif(lens_ == 2):

cur_format = params_[1]

abs_path = os.path.split(os.path.abspath(__file__))

print_file_name(abs_path[0], cur_format)

sys.exit()

else:

cur_path = params_[2]

cur_format = params_[1]

print_file_name(cur_path, cur_format)

调用方式

先将上述代码保存成.py文件,比如保存成print_file_name.py

在命令行调用

打印当前目录所有文件列表

打印当前目录的所有指定格式的文件列表

打印指定目录的指定格式的文件列表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值