本文摘自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()函数将路径拆分为文件名+扩展名
|
以上就是如何查找当前目录和文件目录的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
安卓app可以用Python写吗
Python中psutil库的使用介绍(详细)
Python运算符-实战中常用的三个逻辑运算符使用实例
地理位置geo处理之mysql函数的详细介绍(附代码)
Python怎么读txt文件
Python下载好了怎么打开
Python调用mysql更新数据的方法
Python列表元素如何求和
Python怎么取消注释
Python中is和==有何区别?Python中is和==的区别介绍
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何查找当前目录和文件目录