python操作文件与目录


title: OS文件及目录方法
tags: python os文件 os目录
grammar_cjkRuby: true
---

os.access( ) 方法

os.access(path,mode) 方法用来检测文件或目录的权限,有两个参数:
path: 文件或者目录的路径
mode: 需要检测的权限,其中 mode 的取值有:

  • os.F_OK: mode的参数,检测path是否存在。==在bash中使用[ -f path_to_file ]来检测==
  • os.R_OK: mode的参数, 检测path是否可读。==在bash中通过查看对应用户的读权限==
  • os.W_OK: mode的参数, 检测path是否可写。==在bash中通过查看对应用户的写权限==
  • os.X_OK: mode的参数, 检测path是否可执行。==在bash中通过查看对应用户的可执行权限==
    使用方法:
>>> os.access('/tmp/foo.txt','os.F_OK')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> os.access('/tmp/foo.txt',os.F_OK)
False
# mode参数传入的时候不用加引号
>>> os.access('/tmp',F_OK)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
# mode参数传入的时候,前面还有一个“os”
NameError: name 'F_OK' is not defined
>>> os.access('/tmp',os.F_OK)
True
# path参数可以是一个文件,也可以是一个目录的路径

os.chdir(path)方法

os.access( ) 方法用来改变当前工作目录到指定的路径,有两个参数:
path: 要切换到的新路径
用法如下:

>>> os.chdir('/tmp')
>>> os.getcwd()
'/private/tmp'
>>> os.chdir('/Users/tuyang')
>>> os.getcwd()
'/Users/tuyang'
# os.getcwd()方法用来查看当前的工作目录
# import os,sys

os.chflags(path,flags) 方法

os.access( ) 方法用来置路径的标记为数字标记,多个标记可使用 OR 进行组合,有两个参数:
path: 要切换到的新路径
flags: 可以是以下值:

  • stat.UF_NODUMP: 非转储文件
  • stat.UF_IMMUTABLE: 文件是只读的
  • stat.UF_APPEND: 文件只能追加内容
  • stat.UF_NOUNLINK: 文件不可删除
  • stat.UF_OPAQUE: 目录不透明,需要通过联合堆栈查看
  • stat.SF_ARCHIVED: 可存档文件(超级用户可设)
  • stat.SF_IMMUTABLE: 文件是只读的(超级用户可设)
  • stat.SF_APPEND: 文件只能追加内容(超级用户可设)
  • stat.SF_NOUNLINK: 文件不可删除(超级用户可设)
  • stat.SF_SNAPSHOT: 快照文件(超级用户可设)
    用法:
tutu-de-MacBookPro:workspace tuyang$ touch stat.txt
tutu-de-MacBookPro:workspace tuyang$ ll stat.txt 
-rw-r--r--  1 tuyang  staff  0  9 25 21:03 stat.txt
# 创建一个测试文件

当前的权限是:-rw-r--r--


>>> import os,sys
>>> os.chflags('/Users/tuyang/workspace/stat.txt',stat.UF_APPEND)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'stat' is not defined
# 没有加载stat模块
>>> import stat
>>> os.chflags('/Users/tuyang/workspace/stat.txt',stat.UF_APPEND)
# 执行成功
tutu-de-MacBookPro:workspace tuyang$ echo 111 > stat.txt
-bash: stat.txt: Operation not permitted
# 没有了清空的权限
tutu-de-MacBookPro:workspace tuyang$ echo 111 >> stat.txt
# 但是还可以追加
tutu-de-MacBookPro:workspace tuyang$ cat stat.txt
111
# 文本已经追加都文件中

现在的权限还是是: -rw-r--r-- ,因为修改的是特殊权限
==在Linux中可以使用lsattr命令来查看文件的特殊权限==

os.chown(path, uid, gid)方法

os.chown( ) 方法用来改变当前工作目录到指定的路径,有三个参数:
path: 要修改的文件的路径
uid: user ID
gid: group ID
该方法没有返回值
用法:==使用os.chown()方法修改所有者,所属组==

tutu-de-MacBookPro:workspace tuyang$ id tuyang
uid=501(tuyang) gid=20(staff) ...
# 普通用户的UID GID
tutu-de-MacBookPro:workspace tuyang$ id root
uid=0(root) gid=0(wheel) ...
tutu-de-MacBookPro:workspace tuyang$ ll stat.txt 
-rw-r--r--  1 tuyang(501)  staff(20)  4  9 25 21:15 stat.txt
# root用户的UID GID
tutu-de-MacBookPro:workspace tuyang$ touch stat2.txt
# 在普通用户下创建一个测试文件
tutu-de-MacBookPro:workspace tuyang$ sudo -s
Password:
# 切换到root用户
bash-3.2# python
Python 2.7.15 (default, Aug 22 2018, 16:36:18) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
# 导入os模块
>>> os.chown('/Users/tuyang/workspace/stat2.txt',0,0)
# 使用os.chown()方法修改所有者和所属组
bash-3.2# ls -l /Users/tuyang/workspace/stat2.txt 
-rw-r--r--  1 root  wheel  0  9 25 21:40 /Users/tuyang/workspace/stat2.txt
# 退回到bash查看,已经修改成功

os.chroot(path)方法

os.chown( ) 方法用来更改当前进程的根目录为指定的目录(需要root权限),有三个参数:
path: 要设置为根目录的目录
用法:

>>> os.chroot("/tmp")

os.close(fd) 方法方法

os.close( ) 方法用来关闭指定的文件描述符 fd,有一个参数:
fd: 文件描述符
用法:

>>> import os,sys
# 打开文件
>>> fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
#  写入字符串
>>> os.write(fd, "This is test")
# 关闭文件
>>> os.close(fd)
>>> print (关闭文件成功!!)
  • os.open()方法怎么用?os.write()方法怎么用?os.open()方法怎么用?
    bash-3.2# python
>>> import os,sys
>>> file = os.open('/Users/tuyang/workspace/openfile.txt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes at least 2 arguments (1 given)
# 定义一个打开的文件至少需要2个参数:文件路径和文件打开的权限
>>> file = os.open('/Users/tuyang/workspace/openfile.txt',O_RDWR|O_CREAT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'O_RDWR' is not defined
# 前面需要加上“os”
>>> file = os.open('/Users/tuyang/workspace/openfile.txt',os.O_RDWR|os.O_CREAT)
>>> os.write(file,'this is open file test!')
23
# os.write()方法返回写入的字节数
>>> os.close(file)
tutu-de-MacBookPro:workspace tuyang$ cat openfile.txt 
this is open file test!
# 查看文件内容

猜你喜欢

转载自www.cnblogs.com/tianxie11/p/9763951.html