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章 ...
随机推荐
- [Search Engine] 搜索引擎技术之倒排索引
倒排索引是搜索引擎中最为核心的一项技术之一,可以说是搜索引擎的基石.可以说正是有了倒排索引技术,搜索引擎才能有效率的进行数据库查找.删除等操作. 1. 倒排索引的思想 倒排索引源于实际应用中需要根据属 ...
- iOS开发——UI进阶篇(十九)UISearchBar控件简介
最近用到搜索功能.总结一下 搜索,无疑可以使用UISearchBar控件! 那就先了解一下UISearchBar控件吧! UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你 ...
- poj 1112
昨天晚上看的题. 说实话,我一眼就看出了是二分图,再一眼就看出了是二分图+dp(01背包).但悲剧的是我一眼看出的算法是正确的,但我总以为它是错误的,浪费了很长时间像其他算法(TAT). 今天终于把代 ...
- 两个int的和判断溢出
long a,b; cin>>a>>b; long i; i = a+b; if((i^a)<0 && (i^b)<0) cout<<& ...
- Codeforces 696 D. Legen...
Description 每个字符串有些价值,问生成长度为 \(l\) 的字符串最多能获得多少价值,总字符数不超过 \(200\), \(l\leqslant 10^{14}\) . Sol AC自动机 ...
- Python函数讲解
Python函数
- ASP.NET基础代码备忘
使用ASP.NET原生的__doPostBack方法触发asp:Button //javaScript部分 __doPostBack('<%=btnAmountDivided.UniqueID ...
- PDO和PDOStatement类常用方法
PDO — PDO 类 PDO::beginTransaction — 启动一个事务 PDO::commit — 提交一个事务 PDO::__construct — 创建一个表示数据库连接的 PDO ...
- web应用 http 响应 url uri
动态web 应用结构 WEB-INF --classes --lib web.xml 响应: url uri
- Nginx与Apache的比较
Nginx与Apache的比较 Nginx相对于Apache的优点 轻量级.同样起web服务,比apache占用更少的资源和内存 抗并发.nginx处理请求是异步非阻塞,而apache则是阻塞型.在高 ...