1. 程式人生 > >python操作文件與目錄

python操作文件與目錄

進程 itl 返回值 rec per echo 成功 snap copy


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!
# 查看文件內容

python操作文件與目錄