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

--待续

时间: 2024-08-25 23:19:33

Python os 标准库使用的相关文章

一、Python的标准库String

一.Python的标准库String 1.查看武器 a. help(type()) name = "jane"print(help(type(name))) b. capitalize() name = "jane" print(name.capitalize()) 效果:Jane c. center() name = "jane" print(name.center(50, '-')) 效果:-----------------------jan

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

Python 3标准库 The Python3 Standard Library by  Example -----------------------------------------第十四章     应用构建模块-----------------------------14.1  argparse:命令行选项和参数解析----------------------------- argparse模块 14.1.1  建立解析器 14.1.2  定义参数 argparse模块 14.1.3 

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

今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺眼,就想着拿queue实现,后来,被坑了....于是有了这篇文章.我按照 python标准库之MultiProcessing库的研究 (1) 里面的代码来的,结果就是不断的出错,死过就是不出结果,看看程序: from multiprocessing import Pool, queues impor

python之标准库

Python的标准安装包括一组模块,称为标准库. 10.1 模块 >>>emport math >>>math.sin(0) 0.0 10.1.1 模块是程序 任何python程序都可以作为模块导入. #hello.py print "hello,world!" 解释器在哪里寻找模块.(windows) >>>import sys >>>sys.path.append('c:/python') 在unix系统中,不

python linecache标准库基础学习

#python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件读取多行文本;此模块会在python标准库的其他部分中用到,缓存实现将在内存中保存文件内容(解析为单独的行).API通过索引一个列表返回所请求的行.与反复地读取文件并解析文本来查找所需文本行相比,这样可以节省时间,这个方法在查找同一个文件中多行尤其有用 ,比如一个异常."""im

第十章 Python常用标准库使用(必会)

本章涉及标准库: 1.sys 2.os 3.glob 4.math 5.random 6.platform 7.pikle与cPikle 8.subprocess 9.Queue 10.StringIO 11.logging 12.ConfigParser 13.urllib与urllib2 14.json 15.time 16.datetime 10.1 sys 1)sys.argv 命令行参数. argv[0] #代表本身名字 argv[1] #第一个参数 argv[2] #第二个参数 ar

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

下载地址 中文版(扫描版)请使用迅雷下载 英文版(文字版) 官方文档 The Python Standard Library <Python标准库>一书的目录 <python标准库> 译者序 序 前言 第1章 文本1 1.1 string—文本常量和模板1 1.1.1 函数1 1.1.2 模板2 1.1.3 高级模板4 1.2 textwrap—格式化文本段落6 1.2.1 示例数据6 1.2.2 填充段落6 1.2.3 去除现有缩进7 1.2.4 结合dedent和fill7 1

python的标准库

第三方库放的位置:E:\python\Lib\site-packages 通过命令查询:import sys print (sys.path) 标准库:E:\\python\\lib 第三方库的上一级 import osresult= os.system("dir")print ("返回的结果:",result)当结果为0的时候就说明返回的是执行成功resulta =os.popen("dir").read()这次返回的是结果,而没有加read的

《Python 3标准库》

Python的库太庞大的,确实需要一本书来梳理一下,最好是按库的功能来粗略地分类,方便我们通过目录快速查阅.而<Python3标准库>就是这样一本书.全文1000+页,活脱脱一块大砖头,沉甸甸的,估计得有两三斤重.一共19章,每一章都是按照一个主题来介绍相关的库,每一个库会分几个小节来举例说明.这些例子来源于作者的“Python Module of the Week”开源项目,地址为https://pymotw.com/3/. <Python3标准库>主题: 1.文本: 介绍了st