Python语言零基础入门教程(二十五)

命运对每个人都是一样的,不一样的是各自的努力和付出不同,付出的越多,努力的越多,得到的回报也越多,在你累的时候请看一下身边比你成功却还比你更努力的人,这样,你就会更有动力。

导读:本篇文章讲解 Python语言零基础入门教程(二十五),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Python OS 文件/目录方法

Python语言零基础入门教程(二十四)

39、Python os.openpty() 方法

概述
os.openpty() 方法用于打开一个新的伪终端对。返回 pty 和 tty的文件描述符。

语法
openpty()方法语法格式如下:

os.openpty()

参数

返回值
返回文件描述符对,主从。

实例
以下实例演示了 openpty() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os

# 主 pty, 从 tty
m,s = os.openpty()

print m
print s

# 显示终端名
s = os.ttyname(s)
print m
print s

执行以上程序输出结果为:

3
4
3
/dev/pty0

40、Python os.pathconf() 方法

概述
os.pathconf() 方法用于返回一个打开的文件的系统配置信息。

Unix 平台下可用。

语法
fpathconf()方法语法格式如下:

os.fpathconf(fd, name)

参数
name – 文件描述符

name – 检索的系统配置的值,它也许是一个定义系统值的字符串,这些名字在很多标准中指定(POSIX.1, Unix 95, Unix 98, 和其它)。一些平台也定义了一些额外的名字。这些名字在主操作系统上pathconf_names的字典中。对于不在pathconf_names中的配置变量,传递一个数字作为名字,也是可以接受的。

返回值
返回文件的系统信息。

实例
以下实例演示了 fpathconf() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

print "%s" % os.pathconf_names

# 获取文件最大连接数
no = os.fpathconf(fd, 'PC_LINK_MAX')
print "Maximum number of links to the file. :%d" % no

# 获取文件名最大长度
no = os.fpathconf(fd, 'PC_NAME_MAX')
print "Maximum length of a filename :%d" % no

# 关闭文件
os.close( fd)

print "关闭文件成功!!"

执行以上程序输出结果为:

关闭文件成功!!

41、Python os.pipe() 方法

概述
os.pipe() 方法用于创建一个管道, 返回一对文件描述符(r, w) 分别为读和写。

语法
pipe()方法语法格式如下:

os.pipe()

参数

返回值
返回文件描述符对。

实例
以下实例演示了 pipe() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

print "The child will write text to a pipe and "
print "the parent will read the text written by child..."

# file descriptors r, w for reading and writing
r, w = os.pipe() 

processid = os.fork()
if processid:
    # This is the parent process 
    # Closes file descriptor w
    os.close(w)
    r = os.fdopen(r)
    print "Parent reading"
    str = r.read()
    print "text =", str   
    sys.exit(0)
else:
    # This is the child process
    os.close(r)
    w = os.fdopen(w, 'w')
    print "Child writing"
    w.write("Text written by child...")
    w.close()
    print "Child closing"
    sys.exit(0)

执行以上程序输出结果为:

The child will write text to a pipe and
the parent will read the text written by child...
Parent reading
Child writing
Child closing
text = Text written by child...

42、Python os.popen() 方法

概述
os.popen() 方法用于从一个命令打开一个管道。

在Unix,Windows中有效

语法
popen()方法语法格式如下:

os.popen(command[, mode[, bufsize]])

参数
command – 使用的命令。

mode – 模式权限可以是 ‘r’(默认) 或 ‘w’。

bufsize – 指明了文件需要的缓冲大小:0意味着无缓冲;1意味着行缓冲;其它正值表示使用参数大小的缓冲(大概值,以字节为单位)。负的bufsize意味着使用系统的默认值,一般来说,对于tty设备,它是行缓冲;对于其它文件,它是全缓冲。如果没有改参数,使用系统的默认值。

返回值
返回一个文件描述符号为fd的打开的文件对象

实例
以下实例演示了 popen() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 使用 mkdir 命令
a = 'mkdir nwdir'

b = os.popen(a,'r',1)

print b

执行以上程序输出结果为:

open file 'mkdir nwdir', mode 'r' at 0x81614d0

43、Python os.read() 方法

概述
os.read() 方法用于从文件描述符 fd 中读取最多 n 个字节,返回包含读取字节的字符串,文件描述符 fd对应文件已达到结尾, 返回一个空字符串。

在Unix,Windows中有效

语法
read()方法语法格式如下:

os.read(fd,n)
参数
fd – 文件描述符。

n – 读取的字节。

返回值
返回包含读取字节的字符串

实例
以下实例演示了 read() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys
# 打开文件
fd = os.open("f1.txt",os.O_RDWR)
	
# 读取文本
ret = os.read(fd,12)
print ret

# 关闭文件
os.close(fd)
print "关闭文件成功!!"

执行以上程序输出结果为:

This is test
关闭文件成功!!

44、Python os.readlink() 方法

概述
os.readlink() 方法用于返回软链接所指向的文件。可能返回绝对或相对路径。

在Unix中有效

语法
readlink()方法语法格式如下:

os.readlink(path)

参数
path – 要查找的软链接路径

返回值
返回软链接所指向的文件

实例
以下实例演示了 readlink() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os

src = '/usr/bin/python'
dst = '/tmp/python'

# 创建软链接
os.symlink(src, dst)

# 使用软链接显示源链接
path = os.readlink( dst )
print path

执行以上程序输出结果为:

/usr/bin/python

45、Python os.remove() 方法

概述
os.remove() 方法用于删除指定路径的文件。如果指定的路径是一个目录,将抛出 OSError。

该方法与 unlink() 相同。

在Unix, Windows中有效

语法
remove()方法语法格式如下:

os.remove(path)

参数
path – 要移除的文件路径

返回值
该方法没有返回值

实例
以下实例演示了 remove() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目录
print "目录为: %s" %os.listdir(os.getcwd())

# 移除
os.remove("aa.txt")

# 移除后列出目录
print "移除后 : %s" %os.listdir(os.getcwd())

执行以上程序输出结果为:

目录为:
[ 'a1.txt','aa.txt','resume.doc' ]
移除后 : 
[ 'a1.txt','resume.doc' ]

46、Python os.removedirs() 方法

概述
os.removedirs() 方法用于递归删除目录。像rmdir(), 如果子文件夹成功删除, removedirs()才尝试它们的父文件夹,直到抛出一个error(它基本上被忽略,因为它一般意味着你文件夹不为空)。

语法
removedirs()方法语法格式如下:

os.removedirs(path)

参数
path – 要移除的目录路径

返回值
该方法没有返回值

实例
以下实例演示了 removedirs() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目录
print "目录为: %s" %os.listdir(os.getcwd())

# 移除
os.removedirs("/test")

# 列出移除后的目录
print "移除后目录为 %s :" %os.listdir(os.getcwd())

执行以上程序输出结果为:

目录为:
[  'a1.txt','resume.doc','a3.py','test' ]
移除后目录为:
[  'a1.txt','resume.doc','a3.py' ]

47、Python os.rename() 方法

概述
os.rename() 方法用于命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError。

语法
rename()方法语法格式如下:

os.rename(src, dst)

参数
src – 要修改的文件或目录名

dst – 修改后的文件或目录名

返回值
该方法没有返回值

实例
以下实例演示了 rename() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目录
print "目录为: %s"%os.listdir(os.getcwd())

# 重命名
os.rename("test","test2")

print "重命名成功。"

# 列出重命名后的目录
print "目录为: %s" %os.listdir(os.getcwd())

执行以上程序输出结果为:

目录为:
[  'a1.txt','resume.doc','a3.py','test' ]
重命名成功。
[  'a1.txt','resume.doc','a3.py','test2' ]

48、Python os.renames() 方法

概述
os.renames() 方法用于递归重命名目录或文件。类似rename()。

语法
renames()方法语法格式如下:

os.renames(old, new)

参数
old – 要重命名的目录

new –文件或目录的新名字。甚至可以是包含在目录中的文件,或者完整的目录树。

返回值
该方法没有返回值

实例
以下实例演示了 renames() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys
print "当前目录为: %s" %os.getcwd()

# 列出目录
print "目录为: %s"%os.listdir(os.getcwd())

# 重命名 "aa1.txt"
os.renames("aa1.txt","newdir/aanew.txt")

print "重命名成功。"

# 列出重命名的文件 "aa1.txt"
print "目录为: %s" %os.listdir(os.getcwd())

执行以上程序输出结果为:

当前目录为: /tmp
目录为:
 [  'a1.txt','resume.doc','a3.py','aa1.txt','Administrator','amrood.admin' ]
重命名成功。
目录为:
 [  'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]

49、Python os.rmdir() 方法

概述
os.rmdir() 方法用于删除指定路径的目录。仅当这文件夹是空的才可以, 否则, 抛出OSError。

语法
rmdir()方法语法格式如下:

os.rmdir(path)

参数
path – 要删除的目录路径

返回值
该方法没有返回值

实例
以下实例演示了 rmdir() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目录
print "目录为: %s"%os.listdir(os.getcwd())

# 删除路径
os.rmdir("mydir")

# 列出重命名后的目录
print "目录为: %s" %os.listdir(os.getcwd())

执行以上程序输出结果为:

目录为:
[  'a1.txt','resume.doc','a3.py','mydir' ]
目录为:
[  'a1.txt','resume.doc','a3.py' ]

50、Python os.stat() 方法

概述
os.stat() 方法用于在给定的路径上执行一个系统 stat 的调用。

语法
stat()方法语法格式如下:

os.stat(path)

参数
path – 指定路径

返回值
stat 结构:

st_mode: inode 保护模式
st_ino: inode 节点号。
st_dev: inode 驻留的设备。
st_nlink: inode 的链接数。
st_uid: 所有者的用户ID。
st_gid: 所有者的组ID。
st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
st_atime: 上次访问的时间。
st_mtime: 最后一次修改的时间。
st_ctime: 由操作系统报告的”ctime”。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。

实例
以下实例演示了 stat() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 显示文件 "a2.py" 信息
statinfo = os.stat('a2.py')

print statinfo

执行以上程序输出结果为:

posix.stat_result(st_mode=33188, st_ino=3940649674337682L, st_dev=277923425L, st
_nlink=1, st_uid=400, st_gid=401, st_size=335L, st_atime=1330498089, st_mtime=13
30498089, st_ctime=1330498089)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/143818.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!