手机
当前位置:查字典教程网 >脚本专栏 >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实现文件分组复制到不同目录的例子

python合并文本文件示例

Python删除指定目录下过期文件的2个脚本分享

python 字符串格式化代码

将图片文件嵌入到wxpython代码中的实现方法

python远程登录代码

python实现的udp协议Server和Client代码实例

python将人民币转换大写的脚本代码

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

精品推荐
分类导航