本文摘自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中spyder怎么安装
web自动化测试(二)selenium 3启动ie, firefox,chrome代码示例
Python赋值时大小写敏感吗
Python注释符是什么意思
Python中print与return区别
Python可以运行在jvm上吗
利用Python实现“指尖陀螺”
Python可以做动图吗
了解Python 中日志异步发送到远程服务器
基于Python批量处理dat文件及科学计算的方法
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何查找当前目录和文件目录