Python文件系統功能:os模塊

Python文件系統功能:os模塊python

1.os模塊方法分類mysql

(1)目錄:sql

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

(2)文件:vim

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

(3)訪問權限bash

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)設備文件ssh

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

(5)文件描述符ui

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

四、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模塊code

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'}
相關文章
相關標籤/搜索