Python OS 文件/目录方法

Python os.access() 方法

os.access() 方法使用当前的uid/gid尝试访问路径。大部分操作使用有效的 uid/gid, 因此运行环境可以在 suid/sgid 环境尝试。

os.access(path, mode);

参数

  • path – 要用来检测是否有访问权限的路径。
  • mode – mode为F_OK,测试存在的路径,或者它可以是包含R_OK, W_OK和X_OK或者R_OK, W_OK和X_OK其中之一或者更多。
  • os.F_OK: 作为access()的mode参数,测试path是否存在。
  • os.R_OK: 包含在access()的mode参数中 , 测试path是否可读。
  • os.W_OK 包含在access()的mode参数中 , 测试path是否可写。
  • os.X_OK 包含在access()的mode参数中 ,测试path是否可执行。
    返回值
    如果允许访问返回 True , 否则返回False。
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 假定 /tmp/foo.txt 文件存在,并有读写权限

ret = os.access("/tmp/foo.txt", os.F_OK)
print "F_OK - 返回值 %s"% ret

ret = os.access("/tmp/foo.txt", os.R_OK)
print "R_OK - 返回值 %s"% ret

ret = os.access("/tmp/foo.txt", os.W_OK)
print "W_OK - 返回值 %s"% ret

ret = os.access("/tmp/foo.txt", os.X_OK)
print "X_OK - 返回值 %s"% ret
F_OK - 返回值 True
R_OK - 返回值 True
W_OK - 返回值 True
X_OK - 返回值 False

Python os.chroot() 方法

os.chroot() 方法用于更改当前进程的根目录为指定的目录,使用该函数需要管理员权限。

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

import os, sys

# 设置根目录为 /tmp

os.chroot("/tmp")

print "修改根目录成功!!"

Python os.close() 方法

os.close() 方法用于关闭指定的文件描述符 fd。

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

import os, sys

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

#  写入字符串
os.write(fd, "This is test")

# 关闭文件
os.close( fd )

print "关闭文件成功!!"

Python os.dup() 方法

os.dup() 方法用于复制文件描述符 fd。

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

import os, sys

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

# 复制文件描述符
d_fd = os.dup( fd )

# 使用复制的文件描述符写入文件
os.write(d_fd, "This is test")

# 关闭文件
os.closerange( fd, d_fd)

print "关闭所有文件成功!!"

Python os.dup2() 方法

os.dup2() 方法用于将一个文件描述符 fd 复制到另一个 fd2。

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

import os, sys

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

# 写入字符串
os.write(fd, "This is test")

# 文件描述符为 1000
fd2 = 1000
os.dup2(fd, fd2);

# 在新的文件描述符上插入数据
os.lseek(fd2, 0, 0)
str = os.read(fd2, 100)
print "读取的字符串是 : ", str

# 关闭文件
os.close( fd )

print "关闭文件成功!!"
读取的字符串是 :  This is test
关闭文件成功!!

Python os.fdatasync() 方法

os.fdatasync() 方法用于强制将文件写入磁盘,该文件由文件描述符fd指定,但是不强制更新文件的状态信息。如果你需要刷新缓冲区可以使用该方法。

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

import os, sys

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

# 写入字符串
os.write(fd, "This is test")

# 使用 fdatasync() 方法
os.fdatasync(fd)

# 读取文件
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print "读取的字符是 : ", str

# 关闭文件
os.close( fd )

print "关闭文件成功!!"
读取的字符是 :  This is test
关闭文件成功!!

Python os.fdopen() 方法

os.fdopen() 方法用于通过文件描述符 fd 创建一个文件对象,并返回这个文件对象。

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

import os, sys

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

# 获取以上文件的对象
fo = os.fdopen(fd, "w+")

# 获取当前文章
print "Current I/O pointer position :%d" % fo.tell()

# 写入字符串
fo.write( "Python is a great language.\nYeah its great!!\n");

# 读取内容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print "Read String is : ", str

# 获取当前位置
print "Current I/O pointer position :%d" % fo.tell()

# 关闭文件
os.close( fd )

print "关闭文件成功!!"
Current I/O pointer position :0
Read String is :  This is testPython is a great language.
Yeah its great!!

Current I/O pointer position :45
关闭文件成功!!

Python os.fpathconf() 方法

os.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 "文件最大连接数为 :%d" % no

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

# 关闭文件
os.close( fd )

print "关闭文件成功!!"
{'PC_MAX_INPUT': 2, 'PC_VDISABLE': 8, 'PC_SYNC_IO': 9, 
'PC_SOCK_MAXBUF': 12, 'PC_NAME_MAX': 3, 'PC_MAX_CANON': 1, 
'PC_PRIO_IO': 11, 'PC_CHOWN_RESTRICTED': 6, 'PC_ASYNC_IO': 10, 
'PC_NO_TRUNC': 7, 'PC_FILESIZEBITS': 13, 'PC_LINK_MAX': 0, 
'PC_PIPE_BUF': 5, 'PC_PATH_MAX': 4}

文件最大连接数为 :127
文件名最大长度为 :255
Closed the file successfully!!

Python os.fstat() 方法

os.fstat() 方法用于返回文件描述符fd的状态,类似 stat()。

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

import os, sys

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

# 获取元组
info = os.fstat(fd)

print "文件信息 :", info

# 获取文件 uid
print "文件 UID :%d" % info.st_uid

# 获取文件 gid
print "文件 GID  :%d" % info.st_gid

# 关闭文件
os.close( fd)
文件信息 : (33261, 3753776L, 103L, 1, 0, 0, 
            102L, 1238783197, 1238786767, 1238786767)
文件 UID :0
文件 GID :0

Python os.pipe() 方法

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

#!/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...
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值