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 标准库使用的更多相关文章

  1. 【python】标准库的大致认识

    正如那句 Python 社区中很有名的话所说的:“battery included”,Python 的一大好处在于它有一套很有用的标准库(standard library).标准库是随着 Python ...

  2. python 使用标准库连接linux实现scp和执行命令

    import stat import pexpect 只显示关键代码: sqldb = localpath+database //获取database名字 if os.path.exists(sqld ...

  3. Python的标准库介绍与常用的第三方库

    Python的标准库介绍与常用的第三方库 Python的标准库: datetime:为日期和时间的处理提供了简单和复杂的方法. zlib:以下模块直接支持通用的数据打包和压缩格式:zlib,gzip, ...

  4. Python 3标准库 第十四章 应用构建模块

    Python 3标准库 The Python3 Standard Library by  Example -----------------------------------------第十四章   ...

  5. python常用标准库(os系统模块、shutil文件操作模块)

    常用的标准库 系统模块 import os 系统模块用于对系统进行操作. 常用方法 os模块的常用方法有数十种之多,本文中只选出最常用的几种,其余的还有权限操作.文件的删除创建等详细资料可以参考官方文 ...

  6. python linecache标准库基础学习

    #python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...

  7. python MultiProcessing标准库使用Queue通信的注意要点

    今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...

  8. python 常用标准库

    标准库和第三方库第一手资料: 在线: 官方文档(https://docs.python.org/) 离线:交互式解释器(dir().help()函数),IPython(tab键提示.?.??) 一.  ...

  9. Python:标准库(包含下载地址及书本目录)

    下载地址 英文版(文字版) 官方文档 The Python Standard Library <Python标准库>一书的目录 <python标准库> 译者序 序 前言 第1章 ...

随机推荐

  1. [Scala] akka actor编程(一)

    Akka基础 Akka笔记之Actor简介  Akka中的Actor遵循Actor模型.你可以把Actor当作是人.这些人不会亲自去和别人交谈.他们只通过邮件来交流.  1. 消息传递 2. 并发 3 ...

  2. css3多列样式

  3. PHP-----练习-------租房子-----增删改查,多条件查询

    练习-------租房子-----增删改查,多条件 一 .题目要求: 二 .做法: [1]建立数据库 [2]封装类文件------DBDA.class.php <?php class DBDA ...

  4. 解决java.io.IOException: HTTPS hostname wrong: should be

    原因:当访问HTTPS的网址.您可能已经安装了服务器证书到您的JRE的keystore .但这个错误是指服务器的名称与证书实际域名不相等.这通常发生在你使用的是非标准网上签发的证书. 解决方法:让JR ...

  5. Java的几种常用设计模式

    何为设计模式? 就是对一些常见问题进行归纳总结,并针对具体问题给出一套通用的解决办法(强调的是解决问题的思想): 在开发中,只要遇到这类问题,就可以直接使用这些设计模式解决问题. ---------- ...

  6. AngularJS HTML DOM

    AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令. ng-disabled 指令: ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性 ...

  7. python初学杂记

    python常用命令: 1.python 或者 python3  打开交互式python解释器 2.python hello.py   通过命令提示符运行python脚本 交互式python解释器常用 ...

  8. .NET Remoting 体系结构 之 在 ASP.NET 中驻留远程服务器

    迄今为止,所有服务器示例都是运行在自驻留(self-hosted)的.NET 服务器上.自驻留的服务器必 须手动启动..NET Remoting 服务器也可以在许多其他的应用程序类型中启动.在 Win ...

  9. Hibernate的检索方式

    Hibernate的检索方式 检索方式(查询的方式) 导航对象图检索方式: 根据已经加载的对象导航到其他对象 Customer customer = (Customer)session.get(Cus ...

  10. mysql中,通过脚本设置表的自增列,及自增步长

    设置自增列(其实通过navicate可以直接设置的,也方便:要不然可能需要删除列了) ALTER TABLE `domain_dns_tucows` CHANGE `id` `id` INT(11) ...