Python中的目录与文件判别代码
>>> import os 导入模块
>>> os.listdir("d:\\python25") 列出所有目录和文件
['w9xpopen.exe', 'README.txt', 'NEWS.txt', 'LICENSE.txt', 'python.exe', 'pythonw.exe', 'Lib', 'DLLs', 'include', 'libs', 'tcl', 'Tools', 'Doc', 'odbchelper.py', 'odbchelper.pyc', 'test.log', 'sqlConnection.py', 'sqlConnection.pyc']
>>> dirname="d:\\python25" 支持自定义
>>> os.listdir(dirname)
['w9xpopen.exe', 'README.txt', 'NEWS.txt', 'LICENSE.txt', 'python.exe', 'pythonw.exe', 'Lib', 'DLLs', 'include', 'libs', 'tcl', 'Tools', 'Doc', 'odbchelper.py', 'odbchelper.pyc', 'test.log', 'sqlConnection.py', 'sqlConnection.pyc']
>>> [f for f in os.listdir(dirname) 筛选出一个list,存放filename
if os.path.isfile(os.path.join(dirname, f))]
['w9xpopen.exe', 'README.txt', 'NEWS.txt', 'LICENSE.txt', 'python.exe', 'pythonw.exe', 'odbchelper.py', 'odbchelper.pyc', 'test.log', 'sqlConnection.py', 'sqlConnection.pyc']
>>> [f for f in os.listdir(dirname) 筛选出一个list,存放dirname
if os.path.isdir(os.path.join(dirname, f))]
['Lib', 'DLLs', 'include', 'libs', 'tcl', 'Tools', 'Doc']
判别的应用
>>> os.path.isdir("D:\\")
True
>>> os.path.isdir("D:\\python25\\odbchelper.py")
False
>>> os.path.isfile("D:\\python25\\odbchelper.py")
True
当前目录
>>> os.getcwd()
'D:\\Python25'
通配符的使用,引入glob
IDLE 1.2.1
>>> import glob
>>> glob.glob('D:\\python25\\*.exe')
['D:\\python25\\w9xpopen.exe', 'D:\\python25\\python.exe', 'D:\\python25\\pythonw.exe']
>>> glob.glob('D:\\python25\\py*.exe')
['D:\\python25\\python.exe', 'D:\\python25\\pythonw.exe']
>>>
(资源库 www.zyku.net)
您可能感兴趣的文章
- 04-24一小时学会Python入门教程
- 04-09python实现自动化群控的步骤
- 04-09python 调用js的四种方式
- 04-09Python WSGI 规范简介
- 04-08python实现求纯色彩图像的边框
- 04-08python3使用diagrams绘制架构图的步骤
- 04-08python3 删除所有自定义变量的操作
- 04-08Django 如何实现文件上传下载
- 03-31用python插入独创性声明
- 03-29正确的使用Python临时文件
- 02-25黑鲨4pro桌面开启悬浮球设置教程
- 02-17ASP.NET MVC页面重定向简单介绍
- 10-26微信读书怎么分享书籍给微信好友
- 02-10如何在C++中调用Python
- 09-15iPhone年年焕新规则更改介绍
- 01-13溯源司机-溯源司机应用软件功能介绍
- 12-31口腔医学技术初级师-口腔医学技术初级
- 02-16华为账号解绑手机号操作方法
- 02-19PhpWind教程:检测系统是否支持GD库
- 09-16goodnotes加贴纸方法分享
最近更新
阅读排行
猜你喜欢
- 12-29核工业学院-核工业学院应用软件功能介
- 09-19微信iPad分屏方法介绍
- 02-22DedeCMS首页调用文章正文内容的sql万
- 10-13华为mate30地震预警功能在哪里
- 11-29QQ底部导航在哪里自定义
- 11-28pre 标签内容自动换行的方法
- 03-04wordpress站外以HTML的形式调用wordpr
- 03-02WordPress在LNMP环境下网站二级目录伪
- 02-18移动端html5页面长按实现高亮全选文本
- 02-20Debian系统下安装PHP和Nginx环境