本文摘自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开发的gui漂亮吗
一份Python入门应该看的学习资料
Python保存数组怎么操作
实例介绍Python随机数使用方法,推导以及字符串,双色球
Python语言怎么解决汉诺塔问题
Python中swapcase是什么意思
Python中文件变化监控watchdog的示例
Python注释快捷键是什么
Python怎么升序和降序排序
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何查找当前目录和文件目录