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+'
')
        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'}
原文地址:https://www.cnblogs.com/fanison/p/7115694.html