OS模块

作者: merryzhou | 来源:发表于2017-07-19 22:21 被阅读0次

    记录常用命令:

    • os.name #显示当前使用的平台
    >>> os.name
    'posix'         #unix/Linux,返回posix;windows用户,返回nt
    
    • os.sep #当前平台下路径分隔符
    >>> os.sep
    '/'              #unix/linux
    >>> os.sep
    '\\'             #windows
    
    • os.linesep #当前平台使用的行终止符
    >>> os.linesep
    '\n'            #linux
    >>> os.linesep
    '\r\n'        #windows
    
    • os.environ #获取系统环境变量
    >>> os.environ
    {..., 'PWD': '/Users/merry'}
    
    • os.system() #运行shell命令,这里会打开一个新shell,运行命令,结束后关闭shell
    • os.getcwd() #当前目录
    >>> os.getcwd()
    '/Users/merry'
    
    • os.mkdir(path, [, mode=0777]) #创建一个目录
    • os.makedirs('parent_dir/child_dir') #可生成多层递归目录
    • os.listdir('dirname') #列出指定目录下的所有文件和目录名
    • os.curdir #返回当前目录
    • os.chdir(dirname) #改变当前工作路径
    • os.rename('old_name','new_name') #重命名文件/目录名
    • os.rmdir('dirname') #删除单级目录
    • os.removedirs() #级联删除目录
    >>> os.mkdir('tmp1/tmp2')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [Errno 2] No such file or directory: 'tmp1/tmp2'
    >>> 
    >>> os.mkdir('tmp1')
    >>> os.makedirs('tmp/tmp_file')
    >>> os.listdir('tmp')
    ['tmp_file']
    >>> os.chdir('tmp')
    >>> os.curdir
    '.'
    >>> os.getcwd()
    '/Users/merry/tmp'
    >>> os.listdir('.')
    ['tmp_file']
    >>> os.rename('tmp_file','new')
    >>> os.listdir('.')
    ['new']
    >>> os.makedirs('new/a/b')
    >>> os.rmdir('new')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [Errno 66] Directory not empty: 'new'
    >>> os.removedirs('new')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 170, in removedirs
        rmdir(name)
    OSError: [Errno 66] Directory not empty: 'new'
    >>> os.rmdir('new/a/b')
    >>> os.listdir('./new')
    ['a']
    >>> os.listdir('./new/a')
    []
    >>> os.removedirs('new/a')
    >>> os.listdir('.')
    []
    >>> open('file','w')
    <open file 'file', mode 'w' at 0x1083c35d0>
    >>> open('file.txt','w')
    <open file 'file.txt', mode 'w' at 0x1083c3660>
    >>> os.listdir('.')
    ['file', 'file.txt']
    >>> os.remove('file')
    >>> os.listdir('.')
    ['file.txt']
    
    • os.path.abspath(path) #返回绝对路径
    • os.path.dirname(path) #返回该路径的父目录
    • os.path.basename(path) #返货path最后一个目录或文件,如果以分隔符结尾,则返回空值
    >>> os.path.abspath('a/b')
    '/Users/merry/tmp/a/b'
    >>> os.path.abspath('/a/b')
    '/a/b'
    >>> os.path.dirname('/a/b')
    '/a'
    >>> os.path.dirname('a/b')
    'a'
    >>> os.path.dirname('a/b/')
    'a/b'
    >>> os.path.basename('a/b/')
    ''
    >>> os.path.basename('a/b')
    'b'
    
    • os.path.isfile(path) #判断是否为文件
    • os.path.isdir(path) #判断是否为目录
    >>> os.path.isfile('file.txt')
    True
    >>> os.path.isdir('file.txt')
    False
    
    • os.chmod(path mode) #修改文件的访问权限
    • os.stat() #获取文件或者目录信息
    • os.chown(path, uid, gid) #修改path的owner和group
    >>> os.stat('file.txt')
    posix.stat_result(st_mode=33188, st_ino=3287829, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=0, st_atime=1500473283, st_mtime=1500473273, st_ctime=1500473273)
    >>> os.chmod('file.txt', 0777)
    >>> os.stat('file.txt')
    posix.stat_result(st_mode=33279, st_ino=3287829, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=0, st_atime=1500473283, st_mtime=1500473273, st_ctime=1500473741)
    >>> 
    
    • os.path.split(path) #将path分割成路径名和文件名
    • os.path.join(path, name) #连接目录与文件名或目录,结果为path/name
    • os.path.exists(path) #检测当前路径是否存在
    >>> os.path.split('a/b/c')
    ('a/b', 'c')
    >>> os.path.split('a/b/c/')
    ('a/b/c', '')
    >>> os.path.split('/a/b/c/')
    ('/a/b/c', '')
    >>> os.path.join('a/b','c')
    'a/b/c'
    >>> os.path.exists('a/b/c')
    False
    

    相关文章

      网友评论

          本文标题:OS模块

          本文链接:https://www.haomeiwen.com/subject/rqlkkxtx.html