== os 模块 ==

``os`` 模块为许多操作系统函数提供了统一的接口.

这个模块中的大部分函数通过对应平台相关模块实现, 比如 ``posix`` 和
``nt. os`` 模块会在第一次导入的时候自动加载合适的执行模块. === 处理文件=== 内建的 ``open / file`` 函数用于创建, 打开和编辑文件, 如 [Example - #eg--] 所示. 而
``os`` 模块提供了重命名和删除文件所需的函数. ====Example -. 使用 os 模块重命名和删除文件====[eg--] ```
File: os-example-.py import os
import string def replace(file, search_for, replace_with):
# replace strings in a text file back = os.path.splitext(file)[] + ".bak"
temp = os.path.splitext(file)[] + ".tmp" try:
# remove old temp file, if any
os.remove(temp)
except os.error:
pass fi = open(file)
fo = open(temp, "w") for s in fi.readlines():
fo.write(string.replace(s, search_for, replace_with)) fi.close()
fo.close() try:
# remove old backup file, if any
os.remove(back)
except os.error:
pass # rename original to backup...
os.rename(file, back) # ...and temporary to original
os.rename(temp, file) #
# try it out! file = "samples/sample.txt" replace(file, "hello", "tjena")
replace(file, "tjena", "hello")
``` === 处理目录=== ``os`` 模块也包含了一些用于目录处理的函数. ``listdir`` 函数返回给定目录中所有文件名(包括目录名)组成的列表, 如
[Example - #eg--] 所示. 而 Unix 和 Windows 中使用的当前目录和父目录标记(. 和 .. )不包含在此列表中. ====Example -. 使用 os 列出目录下的文件====[eg--] ```
File: os-example-.py import os for file in os.listdir("samples"):
print file *B*sample.au
sample.jpg
sample.wav
...*b*
``` ``getcwd`` 和 ``chdir`` 函数分别用于获得和改变当前工作目录. 如 [Example - #eg--] 所示. ====Example -. 使用 os 模块改变当前工作目录====[eg--] ```
File: os-example-.py import os # where are we?
cwd = os.getcwd()
print "", cwd # go down
os.chdir("samples")
print "", os.getcwd() # go back up
os.chdir(os.pardir)
print "", os.getcwd() *B* /ematter/librarybook
/ematter/librarybook/samples
/ematter/librarybook*b*
``` ``makedirs`` 和 ``removedirs`` 函数用于创建或删除目录层,如 [Example - #eg--] 所示. ====Example -. 使用 os 模块创建/删除多个目录级====[eg--] ```
File: os-example-.py import os os.makedirs("test/multiple/levels") fp = open("test/multiple/levels/file", "w")
fp.write("inspector praline")
fp.close() # remove the file
os.remove("test/multiple/levels/file") # and all empty directories above it
os.removedirs("test/multiple/levels")
``` ``removedirs`` 函数会删除所给路径中最后一个目录下所有的空目录.
而 ``mkdir`` 和 ``rmdir`` 函数只能处理单个目录级. 如 [Example - #eg--] 所示. ====Example -. 使用 os 模块创建/删除目录====[eg--] ```
File: os-example-.py import os os.mkdir("test")
os.rmdir("test") os.rmdir("samples") # this will fail *B*Traceback (innermost last):
File "os-example-7", line , in ?
OSError: [Errno ] Directory not empty: 'samples'*b*
``` 如果需要删除非空目录, 你可以使用 ``shutil`` 模块中的 ``rmtree`` 函数. === 处理文件属性===
``stat`` 函数可以用来获取一个存在文件的信息, 如 [Example - #eg--]
所示. 它返回一个类元组对象(stat_result对象, 包含 个元素),
依次是st_mode (权限模式), st_ino (inode number), st_dev (device),
st_nlink (number of hard links), st_uid (所有者用户 ID), st_gid
(所有者所在组 ID ), st_size (文件大小, 字节), st_atime (最近一次访问时间),
st_mtime (最近修改时间), st_ctime (平台相关; Unix下的最近一次元数据/metadata修改时间,
或者 Windows 下的创建时间) - 以上项目也可作为属性访问. ``` [!Feather 注: 原文为 元元组. 另,返回对象并非元组类型,为 struct.] ====Example -. 使用 os 模块获取文件属性====[eg--] ```
File: os-example-.py import os
import time file = "samples/sample.jpg" def dump(st):
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
print "- size:", size, "bytes"
print "- owner:", uid, gid
print "- created:", time.ctime(ctime)
print "- last accessed:", time.ctime(atime)
print "- last modified:", time.ctime(mtime)
print "- mode:", oct(mode)
print "- inode/dev:", ino, dev #
# get stats for a filename st = os.stat(file) print "stat", file
dump(st)
print #
# get stats for an open file fp = open(file) st = os.fstat(fp.fileno()) print "fstat", file
dump(st) *B*stat samples/sample.jpg
- size: bytes
- owner:
- created: Tue Sep ::
- last accessed: Sun Sep ::
- last modified: Sun May ::
- mode:
- inode/dev: fstat samples/sample.jpg
- size: bytes
- owner:
- created: Tue Sep ::
- last accessed: Sun Sep ::
- last modified: Sun May ::
- mode:
- inode/dev: *b*
``` 返回对象中有些属性在非 Unix 平台下是无意义的, 比如 (``st_inode`` , ``st_dev``)为
Unix 下的为每个文件提供了唯一标识, 但在其他平台可能为任意无意义数据 . ``stat`` 模块包含了很多可以处理该返回对象的常量及函数. 下面的代码展示了其中的一些. 可以使用 ``chmod`` 和 ``utime`` 函数修改文件的权限模式和时间属性,如 [Example - #eg--] 所示. ====Example -. 使用 os 模块修改文件的权限和时间戳====[eg--] ```
File: os-example-.py import os
import stat, time infile = "samples/sample.jpg"
outfile = "out.jpg" # copy contents
fi = open(infile, "rb")
fo = open(outfile, "wb") while :
s = fi.read()
if not s:
break
fo.write(s) fi.close()
fo.close() # copy mode and timestamp
st = os.stat(infile)
os.chmod(outfile, stat.S_IMODE(st[stat.ST_MODE]))
os.utime(outfile, (st[stat.ST_ATIME], st[stat.ST_MTIME])) print "original", "=>"
print "mode", oct(stat.S_IMODE(st[stat.ST_MODE]))
print "atime", time.ctime(st[stat.ST_ATIME])
print "mtime", time.ctime(st[stat.ST_MTIME]) print "copy", "=>"
st = os.stat(outfile)
print "mode", oct(stat.S_IMODE(st[stat.ST_MODE]))
print "atime", time.ctime(st[stat.ST_ATIME])
print "mtime", time.ctime(st[stat.ST_MTIME]) *B*original =>
mode
atime Thu Oct ::
mtime Mon Nov ::
copy =>
mode
atime Thu Oct ::
mtime Mon Nov :: *b*
``` === 处理进程=== ``system`` 函数在当前进程下执行一个新命令, 并等待它完成, 如 [Example - #eg--] 所示. ====Example -. 使用 os 执行操作系统命令====[eg--] ```
File: os-example-.py import os if os.name == "nt":
command = "dir"
else:
command = "ls -l" os.system(command) *B*-rwxrw-r-- effbot effbot Oct : README
-rwxrw-r-- effbot effbot Oct : SimpleAsyncHTTP.py
-rwxrw-r-- effbot effbot Oct : aifc-example-.py
-rwxrw-r-- effbot effbot Oct : anydbm-example-.py
...*b*
``` 命令通过操作系统的标准 shell 执行, 并返回 shell 的退出状态. 需要注意的是在 Windows /
下, shell 通常是 ``command.com`` , 它的推出状态总是 . 由于 11os.system11 直接将命令传递给 shell , 所以如果你不检查传入参数的时候会很危险
(比如命令 ``os.system("viewer %s" % file)``, 将 file 变量设置为
"``sample.jpg; rm -rf $HOME" ....``). 如果不确定参数的安全性, 那么最好使用
``exec`` 或 ``spawn`` 代替(稍后介绍). ``exec`` 函数会使用新进程替换当前进程(或者说是"转到进程"). 在 [Example - #eg--] 中,
字符串 "goodbye" 永远不会被打印. ====Example -. 使用 os 模块启动新进程====[eg--] ```
File: os-exec-example-.py import os
import sys program = "python"
arguments = ["hello.py"] print os.execvp(program, (program,) + tuple(arguments))
print "goodbye" *B*hello again, and welcome to the show*b*
``` Python 提供了很多表现不同的 ``exec`` 函数. [Example - #eg--] 使用的是
``execvp`` 函数, 它会从标准路径搜索执行程序, 把第二个参数(元组)作为单独的参数传递给程序, 并使用当前的环境变量来运行程序. 其他七个同类型函数请参阅 //Python Library Reference// . 在 Unix 环境下, 你可以通过组合使用 ``exec`` , ``fork`` 以及 ``wait`` 函数来从当前程序调用另一个程序,
如 [Example - #eg--] 所示. ``fork`` 函数复制当前进程, ``wait`` 函数会等待一个子进程执行结束. ====Example -. 使用 os 模块调用其他程序 (Unix)====[eg--] ```
File: os-exec-example-.py import os
import sys def run(program, *args):
pid = os.fork()
if not pid:
os.execvp(program, (program,) + args)
return os.wait()[] run("python", "hello.py") print "goodbye" *B*hello again, and welcome to the show
goodbye*b*
``` ``fork`` 函数在子进程返回中返回 (这个进程首先从 ``fork`` 返回值),
在父进程中返回一个非 的进程标识符(子进程的 PID ). 也就是说,
只有当我们处于子进程的时候 "``not pid``" 才为真. ``fork`` 和 ``wait`` 函数在 Windows 上是不可用的, 但是你可以使用 ``spawn`` 函数,
如 [Example - #eg--] 所示. 不过, ``spawn`` 不会沿着路径搜索可执行文件,
你必须自己处理好这些. ====Example -. 使用 os 模块调用其他程序 (Windows)====[eg--] ```
File: os-spawn-example-.py import os
import string def run(program, *args):
# find executable
for path in string.split(os.environ["PATH"], os.pathsep):
file = os.path.join(path, program) + ".exe"
try:
return os.spawnv(os.P_WAIT, file, (file,) + args)
except os.error:
pass
raise os.error, "cannot find executable" run("python", "hello.py") print "goodbye" *B*hello again, and welcome to the show
goodbye*b*
``` ``spawn`` 函数还可用于在后台运行一个程序. [Example - #eg--] 给 ``run`` 函数添加了一个可选的
``mode`` 参数; 当设置为 ``os.P_NOWAIT`` 时, 这个脚本不会等待子程序结束,
默认值 ``os.P_WAIT`` 时 ``spawn`` 会等待子进程结束. 其它的标志常量还有 ``os.P_OVERLAY`` ,它使得 ``spawn`` 的行为和 ``exec`` 类似,
以及 ``os.P_DETACH`` , 它在后台运行子进程, 与当前控制台和键盘焦点隔离. ====Example -. 使用 os 模块在后台执行程序 (Windows)====[eg--] ```
File: os-spawn-example-.py import os
import string def run(program, *args, **kw):
# find executable
mode = kw.get("mode", os.P_WAIT)
for path in string.split(os.environ["PATH"], os.pathsep):
file = os.path.join(path, program) + ".exe"
try:
return os.spawnv(mode, file, (file,) + args)
except os.error:
pass
raise os.error, "cannot find executable" run("python", "hello.py", mode=os.P_NOWAIT)
print "goodbye" *B*goodbye
hello again, and welcome to the show*b*
``` [Example - #eg--] 提供了一个在 Unix 和 Windows 平台上通用的 ``spawn`` 方法. ====Example -. 使用 spawn 或 fork/exec 调用其他程序====[eg--] ```
File: os-spawn-example-.py import os
import string if os.name in ("nt", "dos"):
exefile = ".exe"
else:
exefile = "" def spawn(program, *args):
try:
# possible 2.0 shortcut!
return os.spawnvp(program, (program,) + args)
except AttributeError:
pass
try:
spawnv = os.spawnv
except AttributeError: # assume it's unix
pid = os.fork()
if not pid:
os.execvp(program, (program,) + args)
return os.wait()[]
else:
# got spawnv but no spawnp: go look for an executable
for path in string.split(os.environ["PATH"], os.pathsep):
file = os.path.join(path, program) + exefile
try:
return spawnv(os.P_WAIT, file, (file,) + args)
except os.error:
pass
raise IOError, "cannot find executable" #
# try it out! spawn("python", "hello.py") print "goodbye" *B*hello again, and welcome to the show
goodbye*b*
``` [Example - #eg--] 首先尝试调用 ``spawnvp`` 函数. 如果该函数不存在
(一些版本/平台没有这个函数), 它将继续查找一个名为 ``spawnv`` 的函数并且
开始查找程序路径. 作为最后的选择, 它会调用 ``exec`` 和 ``fork`` 函数完成工作. === 处理守护进程(Daemon Processes)=== Unix 系统中, 你可以使用 ``fork`` 函数把当前进程转入后台(一个"守护者/daemon"). 一般来说, 你需要派生(fork off)一个当前进程的副本, 然后终止原进程, 如 [Example - #eg--] 所示. ====Example -. 使用 os 模块使脚本作为守护执行 (Unix)====[eg--] ```
File: os-example-.py import os
import time pid = os.fork()
if pid:
os._exit() # kill original print "daemon started"
time.sleep()
print "daemon terminated"
``` 需要创建一个真正的后台程序稍微有点复杂, 首先调用 ``setpgrp`` 函数创建一个 "进程组首领/process group leader". 否则, 向无关进程组发送的信号(同时)会引起守护进程的问题: ``` os.setpgrp() 为了确保守护进程创建的文件能够获得程序指定的 mode flags(权限模式标记?), 最好删除 user mode mask: ``` os.umask() 然后, 你应该重定向 //stdout/stderr// 文件, 而不能只是简单地关闭它们(如果你的程序需要 ``stdout``
或 ``stderr`` 写入内容的时候, 可能会出现意想不到的问题). ```
class NullDevice:
def write(self, s):
pass
sys.stdin.close()
sys.stdout = NullDevice()
sys.stderr = NullDevice()
``` 换言之, 由于 Python 的 ``print`` 和 C 中的 ``printf/fprintf`` 在设备(device)
没有连接后不会关闭你的程序, 此时守护进程中的 ``sys.stdout.write()`` 会抛出一个 //IOError// 异常, 而你的程序依然在后台运行的很好.... 另外, 先前例子中的 ``_exit`` 函数会终止当前进程. 而 ``sys.exit`` 不同, 如果调用者(caller)
捕获了 //SystemExit// 异常, 程序仍然会继续执行. 如 [Example 1-41 #eg-1-41] 所示. ====Example -. 使用 os 模块终止当前进程====[eg--] ```
File: os-example-.py import os
import sys try:
sys.exit()
except SystemExit, value:
print "caught exit(%s)" % value try:
os._exit()
except SystemExit, value:
print "caught exit(%s)" % value print "bye!" *B*caught exit()*b*
```

python标准库介绍——1 os详解的更多相关文章

  1. (转)python标准库中socket模块详解

    python标准库中socket模块详解 socket模块简介 原文:http://www.lybbn.cn/data/datas.php?yw=71 网络上的两个程序通过一个双向的通信连接实现数据的 ...

  2. python标准库之字符编码详解

    codesc官方地址:https://docs.python.org/2/library/codecs.html 相关帮助:http://www.cnblogs.com/huxi/archive/20 ...

  3. python标准库介绍——2 os.path模块详解

    == os.path 模块 == ``os.path`` 模块包含了各种处理长文件名(路径名)的函数. 先导入 (import) ``os`` 模块, 然后就可以以 ``os.path`` 访问该模块 ...

  4. Python标准库--time模块的详解

    time模块 - - -时间获取和转换 在我们学习time模块之前需要对以下的概念进行了解: 时间戳:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08 ...

  5. python标准库中socket模块详解

    包含原理就是tcp的三次握手 http://www.lybbn.cn/data/datas.php?yw=71 这篇讲到了socket和django的联系 https://www.cnblogs.co ...

  6. Python -- 标准库 文件管理 (部分os包,shutil包)

    在操作系统下,用户可以通过操作系统的命令来管理文件,参考linux文件管理相关命令.Python标准库则允许我们从Python内部管理文件.相同的目的,我们有了两条途径.尽管在Python调用标准库的 ...

  7. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  8. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  9. python requests库与json数据处理详解

    1. http://docs.python-requests.org/zh_CN/latest/user/quickstart.html get方法将参数放在url里面,安全性不高,但是效率高:pos ...

随机推荐

  1. ECMAScript5之Object学习笔记(二)

    继续第二部分 Object.freeze(obj) 看字面意思就是“把一个对象冻结”. 下面我们来看个简单的例子以作说明: // a person instance var person = { na ...

  2. (转)Unity中武器与人物的碰撞检测

    自:http://blog.csdn.net/Monzart7an/article/details/24435843 目前来说有三种思路,其实前两种算变种了: 1.动画关键帧回调 + 范围检测. 这个 ...

  3. select设置高度的兼容问题

    在IE678下,我们给select设置高度的话,里面的option无法居中,折中的兼容方式就是,我们给select的border:0:外面套一层div,这个div给他设置padding,让select ...

  4. 在MyEclipse中配置Weblogic10服务器

    MyEclipse中配置Weblogic10服务器 在MyEclipse中配置Weblogic10服务器也是很简单,现在将过程分享给有需要的人. 1.在下方的Server选项卡中,鼠标右键选择“Con ...

  5. npm之使用淘宝源

    暂时使用: npm --registry https://registry.npm.taobao.org i express 永久使用: npm conf set registry https://r ...

  6. CSDN日报20170310——《假如我是一行代码》

    [程序人生]假如我是一行代码 作者:henry-hacker 我们不止一次在生活中听到"假如我如何如何,我会如何如何"的句式.而这种句式说出来的一般意义无非就是让我们站在还有一个角 ...

  7. POSIX 线程详解

    一种支持内存共享的简捷工具 POSIX(可移植操作系统接口)线程是提高代码响应和性能的有力手段.在本系列中,Daniel Robbins 向您精确地展示在编程中如何使用线程.其中还涉及大量幕后细节,读 ...

  8. js 动画性能分析 transfrom

    1.动画实现代码 (1)使用定位实现: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  9. 安装ubuntu后不能从ubuntu引导修复方法

    sudo fdisk -l sudo -i mkdir /media/tempdir mount /dev/sda7 /media/tempdir grub-install --root-direct ...

  10. J2EE项目集成SAP的BO报表

    网上的方案: 每个用户在自己的J2EE系统的用户登陆的同时登陆bo系统,这做法的缺点是登陆bo速度慢,而且如果J2EE用户比较多的话会在bo服务器生成很多的token. 最佳方案(自己研究): 1.调 ...