Python之OS(系统操作)模块常用函数
2024-09-06 02:55:24
mkdir(path[, mode=0777])
makedirs(name,mode=511)
rmdir(path)
removedirs(path)
listdir(path)
getcwd()
chdir(path)
walk(top, topdown=True, onerror=None) 返回一个元组:(遍历的路径,目录,文件列表)
我们来遍历一下文件:
>>> for path, dir, filename in os.walk('/home/branches/python/testdir'):
... for filen in filename:
... os.path.join(path, filen)
...
'/home/branches/python/testdir/f1'
'/home/branches/python/testdir/f2'
'/home/branches/python/testdir/f3'
'/home/branches/python/testdir/jpg/0.ipg'
'/home/branches/python/testdir/jpg/3.ipg'
'/home/branches/python/testdir/jpg/2.ipg'
'/home/branches/python/testdir/jpg/1.ipg'
'/home/branches/python/testdir/jpg/get_img.py'
>>>
>>>
>>>
>>> for path, dir, filename in os.walk('testdir'):
... for filen in filename:
... os.path.join(path, filen)
...
'testdir/f1'
'testdir/f2'
'testdir/f3'
'testdir/jpg/0.ipg'
'testdir/jpg/3.ipg'
'testdir/jpg/2.ipg'
'testdir/jpg/1.ipg'
'testdir/jpg/get_img.py'
>>>
#!/usr/bin/python
#coding:utf8 import os def dirList(path): # 列出当前目录下的完整路径名
filelist = os.listdir(path)
# fpath = os.getcwd() # 它获取的是程序所在路径
allfile = []
for filename in filelist:
# allfile.append(fpath + '/' + filename)
filepath = os.path.join(path, filename) # 用os模块的方法进行路径拼接
if os.path.isdir(filepath):
dirList(filepath)
print filepath allfile = dirList('/home/branches/python/testdir')
最新文章
- 你真的会用 SDWebImage?
- 某Python群的入群题目
- android 38 Abdroid客户端和服务端交互
- 哈哈,CSDN又支持Windows Live Writer了
- 解决SQL Server 占用80端口
- vector 对象中存放指针类型数据
- php使用openssl加密数据
- 打印中文dict list的各种姿势
- 2018.11.30开始学习shader
- 利用"SQL"语句自动生成序号的两种方式
- XML之Well-Formed文档规则
- java多线程快速入门(四)
- java BitSet2
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误几种解决方案
- PAT甲题题解-1005. Spell It Right (20)-数位求和,水
- CTreeCtrl获得鼠标点击时的节点
- 通过yum升级gcc/g++至版本4.8.2
- Guava Files 源码分析(二)
- Unit Test Generator 简介
- javascript 打印错误信息 catch err