网站建设硬件预算,外贸原单童装哪个网站做,产品,织梦程序如何搭建网站Python OS 文件/目录方法
Python语言零基础入门教程#xff08;二十四#xff09;
39、Python os.openpty() 方法
概述 os.openpty() 方法用于打开一个新的伪终端对。返回 pty 和 tty的文件描述符。
语法 openpty()方法语法格式如下#xff1a;
os.openpty()参数 无 返…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/pty040、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, sysprint 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 wos.close(w)r os.fdopen(r)print Parent readingstr r.read()print text , str sys.exit(0)
else:# This is the child processos.close(r)w os.fdopen(w, w)print Child writingw.write(Text written by child...)w.close()print Child closingsys.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() 方法用于从一个命令打开一个管道。
在UnixWindows中有效
语法 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 nwdirb os.popen(a,r,1)print b执行以上程序输出结果为
open file mkdir nwdir, mode r at 0x81614d043、Python os.read() 方法
概述 os.read() 方法用于从文件描述符 fd 中读取最多 n 个字节返回包含读取字节的字符串文件描述符 fd对应文件已达到结尾, 返回一个空字符串。
在UnixWindows中有效
语法 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 ossrc /usr/bin/python
dst /tmp/python# 创建软链接
os.symlink(src, dst)# 使用软链接显示源链接
path os.readlink( dst )
print path执行以上程序输出结果为
/usr/bin/python45、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_mode33188, st_ino3940649674337682L, st_dev277923425L, st
_nlink1, st_uid400, st_gid401, st_size335L, st_atime1330498089, st_mtime13
30498089, st_ctime1330498089)