Python文件系統功能:os模塊


Python文件系統功能:os模塊

1.os模塊方法分類

(1)目錄:

    chdir()         改變工作目錄
    chroot()        設定當前進程的根目錄
    listdir()       列出指定目錄下的所有文件名
    mkdir()         創建指定目錄
    makedirs()      創建多級目錄
    getcwd()        返回當前工作目錄
    rmdir()         刪除指定目錄
    removedirs()    刪除多級目錄

(2)文件:

    mkinfo()        創建管道
    mknod()         創建設備文件
    remove()        刪除文件
    unlink()        刪除鏈接文件
    rename()        重命名
    stat()          返回文件狀態信息
    symlink()       創建符號鏈接
    utime()         更新時間戳
    tmpfile()       創建並打開(w+b)一個新的臨時文件

(3)訪問權限

    access(path, mode)      判斷指定用戶是否有訪問權限      os.access('/tmp',0)   uid為0用戶是否有權限訪問/tmp目錄
    chmod(path,mode)        修改權限        os.chmod('/tmp/s',0640) 將/tmp/s 權限修改為640
    chown(path,uid,gid)     修改屬主、屬組  
    umask()                 設置默認權限模式        os.umask(022)

(4)設備文件

    makedev()       創建設備
    major()         指定設備獲取主設備號
    minor()         指定設備獲取次設備號

(5)文件描述符

    open()          較低的IO打開
    read()          較低的IO讀
    write()         較低的IO寫

4、5相對用的少
補充:
    os.walk()   相當於tree命令
    >>> import os
    >>> a1 = os.walk('/root')
    >>> a1.next()
    ('/root',
     ['.subversion', '.ssh', '.ipython', '.pki', '.cache'],
     ['test.py',
      '.bash_history',
      '.cshrc',
      '.bash_logout',
      '.tcshrc',
      '.bash_profile',
      '.mysql_history',
      '.bashrc',
      '.viminfo'])
    返回一個元組,由(文件名,[文件夾],[文件]) 組成

2.os模塊中的path模塊

1)跟文件路徑相關

    basename()      路徑基名
    dirname()       路徑目錄名
    join()          整合文件名
    split()         返回dirname(),basename()元組
    splitext()      返回(filename,extension)元組
    
    例:
    >>> dir1 = os.path.dirname('/etc/sysconfig/iptables-config')
    >>> dir1
    '/etc/sysconfig'
    >>> file1 = os.path.basename('/etc/sysconfig/iptables-config')
    >>> file1
    'iptables-config'
    >>> os.path.join(dir1,file1)
     '/etc/sysconfig/iptables-config'
    >>> for filename in os.listdir('/tmp'):
            print os.path.join('/tmp',filename)

2)信息

    getatime()      返回文件最近一次訪問時間
    getmtime()      返回文件最近一次修改時間
    getctime()      返回文件最近一次改變時間
    getsize()       返回文件的大小

3)查詢

    exists()        判斷指定文件是否存在    isabs()         判斷指定的路徑是否為絕對路徑
    isdir()         是否為目錄
    isfile()        是否為文件
    islink()        是否符號鏈接
    ismount()       是否為掛載點
    sanefile(f1,f2) 兩個路徑是否指向了同一個文件
    
    例:判斷文件是否存在,存在則打開,讓用戶通過鍵盤反復輸入多行數據,追加保存至此文件中
    >>> import os 
    >>> import os.path
    >>> if os.path.isfile('/tmp/s'):
            f1 = open('/tmp/s','a+')
        while True:
            a2 = raw_input("Input >> ")
            if a2 == 'q' or a2 == 'quit' :
                break
            f1.write(a2+'\n')
        f1.close()

4)對象持久存儲

    把變量從內存中變成可存儲或傳輸的過程稱之為序列化
    pickle、marshal、DBM接口、shelve模塊
    
    pickle   將內存對象持久存儲在文件中
    >>> import pickle
    >>> dict1 = {'x':1,'y':2,'z':'hello world'}
    >>> f1 = open('/tmp/s','a+')
    >>> pickle.dump(dict1,f1)           通過流逝化將字典保存在文件中
    >>> f1.close()
    # file /tmp/s
    /tmp/s: ASCII text
    # cat /tmp/s
    (dp0
    S'y'
    p1
    I2
    sS'x'
    p2
    I1
    sS'z'
    p3
    S'hello world'
    p4
    s.
    >>> f2 = open('/tmp/s','a+')
    >>> dict2 = pickle.load(f2)         重新裝載
    >>> dict2
    {'x':1,'y':2,'z':'hello world'}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM