Python os 标准库使用
os模块是python自带的一个核心模块,用于和操作系统对象进行交互。
1.导入模块获取帮助
>>> import os
>>> help(os)
>>> dir(os)
2.常用方法
2.1 os.sep 获取当前系统的路径分隔符
>>> print os.sep
/
2.2 os.linesep 获取当前平台使用的行终止符
>>> os.linesep
'\n'
2.3 os.name 判断正在使用的平台
Windows 返回 ‘nt'; Linux 返回’posix'
>>> print os.name
posix
2.4 os.getcwd() 获取当前目录
>>> print os.getcwd()
/home/oracle
2.5 os.listdir 列出给定目录里的文件
>>> print os.listdir(os.getcwd())
['.gconfd', '.Trash', '1_dy.sql']
2.6 os.remove() 删除指定的文件
>>> os.remove('/u02/rman_dest2/20151023095720.zip')
2.7 os.rename() 重命名对象名
>>> os.rename('/u02/rman_dest2/20151023/113950.zip','/u02/rman_dest2/20151023/aaa.zip')
2.8 os.rmdir() 删除指定目录
删除不掉非空目录,删除非空目录可以 os.system('rm -rf path') 或 import shutil shutil.rmtree(path)
>>> os.rmdir('/u02/rman_dest2/20151023')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 39] Directory not empty: '/u02/rman_dest2/20151023'
2.9 os.mkdir() 创建指定目录
>>> os.mkdir('/u02/rman_dest2/20151024')
2.10 os.chdir() 改变当前目录
>>> os.chdir('/u02/rman_dest2/20151024')
>>> os.getcwd()
'/u02/rman_dest2/20151024'
2.11 os.system() 执行系统命令
>>> os.system('rm -rf /u02/rman_dest2/20151023')
0
2.12 os.path.exists() 检查指定对象是否存在 True/False
>>> os.path.exists('/u02/rman_dest2/20151023')
False
>>> os.path.exists('/u02/rman_dest2')
True
2.13 os.path.split() 切割给定对象,用来分割路径和文件名
>>> os.path.split('/u02/rman_dest2/aa')
('/u02/rman_dest2', 'aa')
>>> os.path.split('/u02/rman_dest2') #总是切割出最后的
('/u02', 'rman_dest2')
>>> os.path.split('/u02/rman_dest2/')
('/u02/rman_dest2', '')
2.14 os.path.splitext() 分割文件名和扩张名
>>> os.path.splitext('113950.zip')
('113950', '.zip')
2.15 os.path.bashname() 获得给定对象的文件名
>>> os.path.basename('/u02/rman_dest2/aa')
'aa'
>>> os.path.basename('/u02/rman_dest2') #总是获得最后一个
'rman_dest2'
>>> os.path.basename('/u02/rman_dest2/')
''
2.16 os.path.dirname() 获得给定对象的路径
>>> os.path.dirname('/u02/rman_dest2/aa')
'/u02/rman_dest2'
>>> os.path.dirname('/u02/rman_dest2')
'/u02'
>>> os.path.dirname('/u02/rman_dest2/')
'/u02/rman_dest2'
2.17 os.path.abspath() 获得给定对象的决定路径
>>> os.path.abspath('.')
'/u02/rman_dest2/20151024'
>>> os.path.abspath('../')
'/u02/rman_dest2'
>>> os.path.abspath('..')
'/u02/rman_dest2'
2.18 os.path.getsize() 获得给定对象文件的大小
>>> os.path.getsize('/u02/rman_dest2/20151023/113950.zip')
286082025L
2.19 os.path.join(path,name) 连接目录和文件名
>>> os.path.join('/u02/','113950.zip')
'/u02/113950.zip'
>>> os.path.join('/u02','113950.zip')
'/u02/113950.zip'
2.20 os.path.isfile() 判断对象是否为文件 True/False
>>> os.path.isfile('/u02/rman_dest2/20151023/113950.zip')
True
>>> os.path.isfile('/u02/113950.zip') #该文件就不存在
False
>>> os.path.isfile('/u02')
False
2.21 os.path.isdir() 判断对象是否为目录 True/False
>>> os.path.isdir('/u02/rman_dest2/20151023/113950.zip')
False
>>> os.path.isdir('/u02/113950.zip')
False
>>> os.path.isdir('/u02')
True
--待续
Python os 标准库使用的更多相关文章
- 【python】标准库的大致认识
正如那句 Python 社区中很有名的话所说的:“battery included”,Python 的一大好处在于它有一套很有用的标准库(standard library).标准库是随着 Python ...
- python 使用标准库连接linux实现scp和执行命令
import stat import pexpect 只显示关键代码: sqldb = localpath+database //获取database名字 if os.path.exists(sqld ...
- Python的标准库介绍与常用的第三方库
Python的标准库介绍与常用的第三方库 Python的标准库: datetime:为日期和时间的处理提供了简单和复杂的方法. zlib:以下模块直接支持通用的数据打包和压缩格式:zlib,gzip, ...
- Python 3标准库 第十四章 应用构建模块
Python 3标准库 The Python3 Standard Library by Example -----------------------------------------第十四章 ...
- python常用标准库(os系统模块、shutil文件操作模块)
常用的标准库 系统模块 import os 系统模块用于对系统进行操作. 常用方法 os模块的常用方法有数十种之多,本文中只选出最常用的几种,其余的还有权限操作.文件的删除创建等详细资料可以参考官方文 ...
- python linecache标准库基础学习
#python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...
- python MultiProcessing标准库使用Queue通信的注意要点
今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...
- python 常用标准库
标准库和第三方库第一手资料: 在线: 官方文档(https://docs.python.org/) 离线:交互式解释器(dir().help()函数),IPython(tab键提示.?.??) 一. ...
- Python:标准库(包含下载地址及书本目录)
下载地址 英文版(文字版) 官方文档 The Python Standard Library <Python标准库>一书的目录 <python标准库> 译者序 序 前言 第1章 ...
随机推荐
- 只用@property定义一个属性speed,子类不能直接用_speed,需要在interface的成员变量列表里写上_speed
//写法一: @interface Person : NSObject { } @property (nonatomic, strong) NSString *name; @end @implemen ...
- 【bzoj2318】Spoj4060 game with probability Problem
题目描述 Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬币,如果正面朝上,则从n个石子中取出一个石子,否则不做任何事.取到最后一颗石子的人胜利.Alice在投掷硬币时有 ...
- java.lang.UnsatisfiedLinkError: Couldn't load BaiduMapSDK 的解决方法
遇到找不到so的同学们可以先从以下几个方面来检查问题: 1.so的名字是不是被修改了?我们SDK的so名字是固定的,如果您自行对它进行了重命名操作,那肯定是没法找到so的.2.so放置位置不对.so需 ...
- 【ZJOI2007】棋盘制作 BZOJ1057
Description 国 际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方 阵,对应八八六十四卦,黑白对 ...
- Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
今天遇到了Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]这个错误,一直也没有百度,不料想却弄了一个 ...
- 数据库DBA(3年以内需求)
1.DBA工作:日常维护,备份及恢复,系统安装,补丁应用,健康检查及优化,故障处理; 2.精通sql语句.视图存储过程.函数的编写.触发器; 3.精通data guard/RAC,能顺利配置和管理da ...
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds -----》myeclipse2015
错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错 ...
- poi导出word、excel
在实际的项目开发中,经常会有一些涉及到导入导出的文档的功能.apache开源项目之一poi对此有很好的支持,对之前的使用做一些简要的总结. 1,导入jar 为了保证对格式的兼容性,在项目的pom.xm ...
- 字符串数组初始化0 与memset 0 效率的分析
转自:http://www.xuebuyuan.com/1722207.html 结合http://blog.sina.com.cn/s/blog_59d470310100gov8.html来看. 最 ...
- Redis到底该如何利用(二)?
上一篇文章里我简述了使用Keys作为Redis搜索的方式,确实感受到了社区的力量,写文章好处多.首先谢谢各位前辈的指导,我知道了拿Redis作为搜索是个错误的方向.本来这篇文章我觉得确实没必要发了,但 ...