Python文件操作与目录
来源:互联网 发布:淘宝让投诉盗图后果 编辑:程序博客网 时间:2024/06/05 21:07
1.获取当前目录下的所有目录:
[x for x in os.listdir('.') if os.path.isdir(x)]
其中,左边x表示列表元素,for...in..
表示遍历目录下的所有对象。if
表示判断True则返回。
2.获取当前目录下的所有py文件:
[x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
增加判断条件,其中涉及常用模块:os.path.splitext(x)[1]
表示分离文件名和扩展名。例如os.path.splitext('F:\Python\聚类分析.py')
打印结果为 ('F:\\Python\\聚类分析', '.py')
所以‘.py’表示判断。
3.os.path.join()
将两个文件路径合并成一个。
例如:os.path.join('home','fan','python')
返回值为:
'home\\fan\\python'
阅读全文
0 0
- python:目录与文件操作
- python目录与文件操作
- python:目录与文件操作
- python 文件与目录操作
- python 文件与目录操作
- Python 文件与目录操作
- Python 文件与目录操作
- python文件与目录操作
- Python 文件与目录操作 .
- Python目录与文件操作
- Python 文件与目录操作
- python文件与目录操作
- Python文件操作与目录
- Python字符串、文件与目录操作
- python文件目录操作
- python文件目录操作
- Python 文件、目录操作
- python 目录,文件操作
- Mongodb 基础(Z)
- The APK file does not exist on disk
- WPF DataGrid 性能加载大数据
- unity物体的移动/脚本的添加
- C#操作XML总结
- Python文件操作与目录
- 基于Grub 2.00的x86内核引导流程--源代码情景分析(1)
- JDK之Arrays类
- Unity UGUI 原理篇 (一):Canvas 渲染模式
- ServiceStack.Redis订阅发布服务的调用(Z)
- 2017多校联合四1009/hdu 6075Questionnaire思维
- .NetChajian
- 性能优化方法(Z)
- web第四天js