本文摘自php中文网,作者anonymity,侵删。
os模块下有两个函数: os.walk()
os.listdir()

1 2 3 4 5 6 7 | # -*- coding: utf-8 -*-
import os
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
print (root) #当前目录路径
print (dirs) #当前路径下所有子目录
print (files) #当前路径下所有非目录子文件
|
输出格式为:
当前文件目录路径
当前路径下子文件目录(若存在, 不存在则为 [] )
当前路径下非目录子文件(仅为子文件的文件名)
案例:
1 2 3 4 5 6 7 8 9 10 11 12 | # -*- coding: utf-8 -*-
import os
def file_name(file_dir):
L=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.jpeg' :
L.append(os.path.join(root, file))
return L
#其中os.path.splitext()函数将路径拆分为文件名+扩展名
|
以上就是如何查找当前目录和文件目录的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python常用类型转换实现
Python字典可变吗
Python编程json格式的转换、else语句的活用和setdefault方法详解
Python判断变量的类型吗
基于Python中staticmethod和classmethod的区别
Python的数据结构
Python按哪个键运行
Python递归求阶乘的方法
Python实现可变变量名
Python sort函数怎么用
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何查找当前目录和文件目录