py中的目录与文件判别代码
所属分类:
脚本专栏 / python
阅读数:
615
收藏 0赞 0分享
>>> 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']
>>>
Python udp网络程序实现发送、接收数据功能示例
这篇文章主要介绍了Python udp网络程序实现发送、接收数据功能,结合实例形式分析了Python使用socket模块进行udp套接字创建、数据收发等相关操作技巧,需要的朋友可以参考下
收藏 0赞 0分享
Python远程开发环境部署与调试过程图解
这篇文章主要介绍了Python远程开发环境部署与调试过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0赞 0分享
Python箱型图处理离群点的例子
今天小编就为大家分享一篇Python箱型图处理离群点的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0赞 0分享
YUV转为jpg图像的实现
今天小编就为大家分享一篇YUV转为jpg图像的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0赞 0分享
Python 中如何实现参数化测试的方法示例
这篇文章主要介绍了Python 中如何实现参数化测试的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
查看更多