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章 ...
随机推荐
- unix编程书中的 ourhdr.h代码
真心不知到里面写的具体什么意思,先记下吧. /*Our own header, to be included after all standard system headers*/ #ifndef _ ...
- [Search Engine] 搜索引擎分类和基础架构概述
大家一定不会多搜索引擎感到陌生,搜索引擎是互联网发展的最直接的产物,它可以帮助我们从海量的互联网资料中找到我们查询的内容,也是我们日常学习.工作和娱乐不可或缺的查询工具.之前本人也是经常使用Googl ...
- PHP变量入门教程(4)PHP 的外部变量
PHP 的外部变量 HTML 表单(GET 和 POST) 当一个表单体交给 PHP 脚本时,表单中的信息会自动在脚本中可用.有很多方法访问此信息,例如: 一个简单的 HTML 表单 <form ...
- 利用 PhpStorm、Idea 等 IDE 如何 运行/调试 Go 程序 ?
以自己常用的 PhpStorm 为例 第一步:下载安装 Go插件 File -> Settings -> Plugins -> 输入关键字:Go 第二步:新建 Go项目 File - ...
- Windows下安装MongoDB
项目当中用到MongoDB最为NoSQL数据库,运行的平台为 Windows Server 2008,下面是MongoDB的安装过程笔记: 1.下载软件 官方下载地址:http://www.mongo ...
- 第3月第13天 cpp模版 Iterator模式 proactor
1.模版除了传参,还可以自动创建.而传指针只是传参而已. template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_P ...
- ngnix 配置CI框架 与 CI的简单使用
ngnix 支持 CI框架1.修改config.php 参考网址:https://www.chenyudong.com/archives/codeigniter-in-nginx-and-url-re ...
- java基础 字符串 “==” 和 “equals” 比较
demo: public class TestStringEquals { public static void main(String[] args) { String a = "test ...
- C++基础知识(4)---例外、异常处理
对Java熟悉的朋友们都很清楚,java中的异常处理机制是非常完善的.并且java强制使用异常处理,用户必须对有可能出现异常的情况进行处理. 在C++中并没有强制用户使用异常处理,但是使用异常处理将会 ...
- opencv常见代码
http://blog.csdn.net/lyc_daniel/article/details/16883707