手机
当前位置:查字典教程网 >脚本专栏 >python >py中的目录与文件判别代码
py中的目录与文件判别代码
摘要:>>>importos导入模块>>>os.listdir("d:python25")列出所有目录和文件['w9xpopen.exe','RE...

>>>importos导入模块

>>>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']

>>>[fforfinos.listdir(dirname)筛选出一个list,存放filename

ifos.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']

>>>[fforfinos.listdir(dirname)筛选出一个list,存放dirname

ifos.path.isdir(os.path.join(dirname,f))]

['Lib','DLLs','include','libs','tcl','Tools','Doc']

判别的应用

>>>os.path.isdir("D:")

True

>>>os.path.isdir("D:python25odbchelper.py")

False

>>>os.path.isfile("D:python25odbchelper.py")

True

当前目录

>>>os.getcwd()

'D:Python25'

通配符的使用,引入glob

IDLE1.2.1

>>>importglob

>>>glob.glob('D:python25*.exe')

['D:python25w9xpopen.exe','D:python25python.exe','D:python25pythonw.exe']

>>>glob.glob('D:python25py*.exe')

['D:python25python.exe','D:python25pythonw.exe']

>>>

【py中的目录与文件判别代码】相关文章:

python 字符串格式化代码

python str与repr的区别

python中mechanize库的简单使用示例

python 中文乱码问题深入分析

python实现的登录和操作开心网脚本分享

Python中lambda的用法及其与def的区别解析

删除目录下相同文件的python代码(逐级优化)

python操作xml文件详细介绍

Python中关键字is与==的区别简述

python实现跨文件全局变量的方法

精品推荐
分类导航