这篇文章主要介绍了Python os模块介绍,需要的朋友可以参考下
来源:互联网 发布:office2016 mac 10.9 编辑:程序博客网 时间:2024/06/05 04:45
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.curdir 返回当前目录: ('.')
1
os.pardir 获取当前目录的父目录字符串名:(
'..'
)
os.makedirs('dirname1/dirname2') 可生成多层递归目录
os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
os.stat('path/filename') 获取文件/目录信息
os.symlink('path/filename','ln_filename') 创建符号链接,源需绝对路径
os.utime() 修改时间属性>>> import os>>> stinfo = os.stat('c.py')>>> print "access time of c.py: %s \nmodified time of c.py: %s" % (stinfo.st_atime,stinfo.st_mtime)access time of c.py: 1375448908.0modified time of c.py: 1369735909.0>>> os.utime('c.py',(1375448978,1369735977))>>> print "access time of c.py: %s \nmodified time of c.py: %s" % (stinfo.st_atime,stinfo.st_mtime)access time of c.py: 1375448908.0modified time of c.py: 1369735909.0退出Python交互模式,再次进入>>> import os>>> stinfo = os.stat('c.py')>>> print "access time of c.py: %s \nmodified time of c.py: %s" % (stinfo.st_atime,stinfo.st_mtime)access time of c.py: 1375448978.0modified time of c.py: 1369735977.0
os.walk() 生成一个目录树下的所有文件名
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
- top表示需要遍历的目录树的路径
- topdown的默认值是”True”,表示首先返回目录树下的文件,然后在遍历目录树的子目录.Topdown的值为”False”时,则表示先遍历目录树的子目录,返回子目录下的文件,最后返回根目录下的文件
- onerror的默认值是”None”,表示忽略文件遍历时产生的错误.如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历
该函数返回一个元组,该元组有3个元素,这3个元素分别表示每次遍历的路径名,目录列表和文件列表
os.walk()举例:
1
2
3
4
5
6
7
>>>
import
os
>>>
for
root, dirs, files
in
os.walk(
"wd/chat"
, topdown
=
False
):
...
for
name
in
files:
...
print
(os.path.join(root, name))
#打印文件绝对路径
...
for
name
in
dirs:
...
print
(os.path.join(root, name))
#打印目录绝对路径
...
os.tmpfile() 创建并打开‘w+b'一个新的临时文件
os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep 输出用于分割文件路径的字符串
os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.popen("bash command") 运行shell命令,生成对象,可赋给变量,再用read读取>>> import os>>> os.system('ls twisted')chat_client_twisted.py chat_server_twisted.py0 >>> LS = os.popen('ls twisted')>>> LS.readlines()['chat_client_twisted.py\n', 'chat_server_twisted.py\n']
os.environ 获取系统环境变量
os.access('pathfile',os.W_OK) 检验文件权限模式,输出True,False
os.chmod('pathfile',os.W_OK) 改变文件权限模式
# echo 'test' > test.sh>>> os.access('test.sh',os.W_OK)True>>> os.access('test.sh',os.X_OK)False>>> os.chmod('test.sh',os.X_OK)>>> os.access('test.sh',os.X_OK)True# ls -l test.sh---------x 1 root root 12 Oct 20 23:03 test.sh
os.path常用模块详解
os.path.abspath(path) 返回path规范化的绝对路径>>> import os.path>>> os.path.abspath('c.py')'/root/py/c.py'>>> os.path.abspath('../py/c.py')'/root/py/c.py'
os.path.split(path) 将path分割成目录和文件名二元组返回>>> os.path.split('/root/py/c.py')('/root/py', 'c.py')>>> os.path.split('/root/py/')('/root/py', '')
os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素>>> os.path.dirname('/root/py/c.py')'/root/py'>>> os.path.dirname('c.py')''
os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素>>> os.path.basename('/root/py/c.py')'c.py'>>> os.path.basename('/root/py')'py'
os.path.commonprefix(list) 返回list中,所有path共有的最长的路径,从左向右,相同字符
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.normcase(path) 在Linux下,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为反斜杠>>> os.path.normcase('c:/windows\\system32\\')'c:\\windows\\system32\\'
os.path.normpath(path) 规范化路径>>> os.path.normpath('c://windows\\System32\\../Temp/')'c:\\windows\\Temp'
os.path.splitdrive(path) 拆分驱动器名和路径,主要对win,对linux元组第一个总是空的>>> os.path.splitdrive('c:\\windows')('c:', '\\windows')
os.path.splitext(path) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作 ,以“.”为分隔符>>> os.path.splitext('/root/py/c.py')('/root/py/c', '.py')
os.path.getsize(path) 返回path的大小(字节)
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
os.path.walk(top,func,arg)
- top表示需要遍历的目录树的路径
- func表示回调函数,对遍历路径进行处理.所谓回调函数,是作为某个函数的参数使用,当某个时间触发时,程序将调用定义好的回调函数处理某个任务.回调函数必须提供3个参数:第1个参数为walk()的参数tag,第2个参数表示目录列表,第3个参数表示文件列表
- arg是传递给回调参数func的元组.回调函数的一个参数必须是arg,为回调函数提供处理参数.参数arg可以为空
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>>
import
os
>>>
def
VisitDir(arg,dirname,names):
...
for
filespath
in
names:
...
print
os.path.join(dirname,filespath)
...
>>> path
=
'/root/py/wd/chat'
>>> os.path.walk(path,VisitDir,())
/
root
/
py
/
wd
/
chat
/
chat_server.py
/
root
/
py
/
wd
/
chat
/
chat_client.py
/
root
/
py
/
wd
/
chat
/
test
/
root
/
py
/
wd
/
chat
/
test
/
linuxeye
/
root
/
py
/
wd
/
chat
/
test
/
test2
/
root
/
py
/
wd
/
chat
/
test
/
test3
/
root
/
py
/
wd
/
chat
/
test
/
test2
/
asdf
/
root
/
py
/
wd
/
chat
/
test
/
test3
/
sdfaxx
阅读全文
0 0
- 这篇文章主要介绍了Python os模块介绍,需要的朋友可以参考下
- 这篇文章主要介绍了PHP整合七牛实现上传文件的相关资料,需要的朋友可以参考下
- 这篇文章主要介绍了python中的实例方法、静态方法、类方法、类变量和实例变量浅析,需要的朋友可以参考下
- 这篇文章主要介绍了C#中的匿名方法,包括其由来、定义及用法等,需要的朋友可以参考下
- 这篇文章主要介绍了Apache2.4.x版wampserver本地php服务器如何让外网访问及启用.htaccess,需要的朋友可以参考下
- 这篇文章主要介绍了JavaWeb中的文件上传和下载功能的实现,在Web应用系统开发中,文件上传和下载功能是非常常用的功能,需要的朋友可以参考下
- 这篇文章主要介绍了Apache2.4.x版wampserver本地php服务器如何让外网访问及启用.htaccess,需要的朋友可以参考下 Apache 从2.2升级到 Apache2.4.x 后配置
- Python 主要模块介绍
- 这篇文章主要介绍了python中enumerate的用法,对Python初学者而言是非常重要的概念
- 这篇文章给大家介绍Android开发四大组件之实现电话拦截和电话录音,涉及到android四大基本组件在程序中的应用,对android四大基本组件感兴趣的朋友可以参考下本篇文章
- Python os模块介绍
- Python os 模块介绍
- Python os模块介绍
- Python os模块介绍
- Python os模块介绍
- python 的os模块下chmod方法简单介绍
- 这篇文章主要介绍了Python使用函数默认值实现函数静态变量的方法,是很实用的功能
- python在不同层级目录import模块的方法 作者:运维之路 字体:[增加 减小] 类型:转载 时间:2016-01-31 我要评论 这篇文章主要介绍了python 在不同层级目录import
- 任务的创建、挂起和恢复
- 从零开始动手写个公司内部用的iOS App
- Mongoose初体验
- spring Boot 设置tomcat端口号
- 友盟推送集成小米华为通道
- 这篇文章主要介绍了Python os模块介绍,需要的朋友可以参考下
- 基于Spring boot,使用idea方便地切换启动环境
- java字符串与数字的分割
- 知识付费水涨船高,某公众号挂羊头卖狗肉赚千万
- java se 四大块 之线程(下)
- yum源地址
- Camshift与Meanshift算法
- 【EXPDP】expdp/impdp数据泵远程导入导出
- 欢迎使用CSDN-markdown编辑器