python学习_24(目录)

发布时间:2019-07-01 12:11:13编辑:auto阅读(1221)

    os.curdir
    返回当前目录
    >>> os.curdir
    '.'
    
    os.pardir
    返回当前目录的父目录
    >>> os.pardir
    '..'
    #切换到上级目录
    >>> os.chdir(os.pardir)
    >>> os.getcwd()
    'E:\\'
    
    os.getcwd()
    获取当前工作目录
    
    >>> os.getcwd()
    'E:\\'
    >>> os.chdir("个人")
    >>> os.getcwd()
    'E:\\个人'
    
    os.chdir(path)
    切换到指定工作目录
    >>> os.chdir("e:\\python")
    >>> os.getcwd()
    'e:\\python'
    
    os.name
    获取当前使用的操作系统类型(其中 ‘nt’ 是 windows,’posix’ 是linux 或者 unix)
    
    >>> os.name
    'nt'
    
    os.mkdir(path [, mode=0777])
    生成单级目录;相当于linux中的mkdir dirname
    
    >>> os.mkdir("1212")
    
    >>> os.chdir("1212")
    >>> os.getcwd()
    'e:\\python\\1212'
    
    os.makedirs(path [, mode=0777])
    可生成多层递归目录,父目录如果不存在,递归生成。
    >>> os.makedirs("e:\\e\\e")
    >>> os.chdir("e:\\e\\e")
    >>> os.getcwd()
    'e:\\e\\e'
    
    os.removedirs(path)
    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,如果目录不为空则不删除
    
    >>> os.removedirs("e:\\e\\e")
    
    os.rmdir(path)
    删除单级非空目录,若目录不为空则无法删除,会报错
    
    >>> os.mkdir("11111")
    >>> os.rmdir("11111")
    
    os.listdir(path)
    列出目录下的所有文件和子目录,包括隐藏文件和目录,返回包含文件和子目录的列表
    
    >>> os.listdir("e:\\python")
    ['0.txt', '1.txt', '10.txt', '1008.txt', '101401.txt', '1212', '2.txt', '3.txt', '4.txt', '
    ess.py', 'process.pyc', 'redata.txt', 'restful.py', 'singleton.py', 'test.py', 'w.txt']
    
    os.remove(filePath)
    删除一个文件,只能删除文件不能删除目录,filePath为文件路径,可以为绝对路径,也可以为相对路径
    >>> os.chdir("e:\\python\\")
    >>> os.remove("1.txt")
    
    os.rename(oldname, newname)
    重命名文件和目录
    
    >>> os.rename("2.txt","232323.txt")#文件
    
    >>> os.rename("e:\\python2","e:\\python2222")#目录
    
    os.stat(path)
    获取文件的一些属性值
    
    >>> os.stat("3.txt")
    os.stat_result(st_mode=33206, st_ino=281474976711192, st_dev=105876, st_nlink=1, st_uid=0, st_gid=0, st_size=201, st_atime=1534340733, st_mtime=1539184690, st_ctime=1534340733)
    
    st_size 文件大小,字节
    st_atime 文件的访问时间
    st_mtime 文件的修改时间啊
    st_ctime 文件的创建时间
    
    访问属性值
    
    >>> os.stat("3.txt").st_ctime
    1534340733.0906618
    
    os.utime(path[, (atime, mtime)])
    修改文件访问时间、修改时间
    #encoding =utf-8
    
    import os
    
    os.utime(r'e:\b.txt',(1375448978,1369735977))
    fileinfo = os.stat(r'e:\b.txt')
    print ("access time of b.txt : %s \n modified time of b.txt: %s"  % 
    
    (fileinfo.st_atime,fileinfo.st_mtime))
    
    os.system (command)
    执行系统命令
    
    >>> os.system("dir e:\\python")
    os.linesep
    操作系统的换行分隔符
    >>> os.linesep  
    '\r\n'
    
    os.pathsep
    路径之间分割符
    如:c:\\python;d:\\python
    >>> os.pathsep
    ';'
    os.sep
    操作系统的路径分割符,如e:\\python\\3.txt
    
    >>> os.sep
    '\\'
    
    os.access(path, mode)
    输出文件权限模式
    import os
    with open(r"c:\gloryroadtest.txt","w") as f:pass
    print (os.access(r'c:\gloryroadtest.txt', os.W_OK))
    print (os.access(r'c:\gloryroadtest.txt', os.R_OK))
    print (os.access(r'c:\gloryroadtest.txt', os.X_OK))
    
    W写,R读,X可执行,
    
    os.chmod(path, mode)
    Linux上会有效,设定文件权限为可读、可写和可执行
    >>> os.chmod("e:\\python\\3.txt",777)
    
    os.popen(command [, mode='r' [, bufsize]])
    运行shell命令,并返回一个文件对象。然后通过操作文件的方法去操作这个
    文件对象。
    >>> for v in os.popen("dir"):
    ...     print(v)
    ...
    
    os.walk(top,topdown=True,onerror=None, followlinks=False)
    ➢top:表示需要遍历的目录树的路径。- 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames, filenames)【文件夹路径, 文件夹名字, 文件名】
    ➢topdown的默认值是“True”,表示首先返回目录树下的文件,然后遍历目录树下的
    子目录。值设为False时,则表示先遍历目录树下的子目录,返回子目录下的文件,最后
    返回根目录下的文件。
    topdown=False 从最底层目录开始遍历
    topdown=True 从最顶层目录开始遍历
    
    ➢onerror的默认值是“None”,表示忽略文件遍历时产生的错误。如果不为空,则提
    供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。
    ➢该函数返回一个列表,列表中的每一个元素都是一个元组,该元组有3个元素,分别表
    示每次遍历的路径名,目录列表和文件列表。
    ➢默认情况下,os.walk 不会遍历软链接指向的子目录,若有需要请将followlinks设定为
    true
    >>> for root,dirs,files in os.walk("e:\\testdemo"):
    ...     for dir in dirs:
    ...         print(dir)
    ...     for file in files:
    ...         print(file)
    ...
    root,dirs,files是 一个三元组,root,为文件夹路径,dirs为文件夹下的子目录列表,files为文件夹路径下的文件列表,每一个文件夹都会产生自己的三元组,针对每个文件夹都会遍历下面的子目录和文件;
    
    #encoding=utf-8
    import os
    for root,dirs,files in os.walk("e:\\python"):
         print("当前目录:",root)
         for dir in dirs:
             print("目录名:",dir)
         for file in files:
             print("文件名:",os.path.join(root,file))
    
    os.path.abspath(path)
    返回绝对路径,但是文件不一定存在
    >>> os.getcwd()
    'e:\\python'
    
    >>> os.path.abspath("3.txt")
    'e:\\python\\3.txt'
    
    os.path.split(path)
    将path分割成路径名和文件名,返回一个元组,如果path是一个路径不包含命名,会将最后一个目录作为文件名分离
    >>> os.path.split("e:\\python\\3.txt")
    
    ('e:\\python', '3.txt')#元组
    >>> os.path.split("e:\\python")
    ('e:\\', 'python')
    os.path.splitext(path)
    分离文件名与扩展名,返回一个元组
    >>> os.path.splitext("e:\\python\\3.txt")
    ('e:\\python\\3', '.txt')
    os.path.dirname(path)
    返回path的目录路径,就是os.path.split(path)的第一个元素
    >>> os.path.dirname("e:\\python\\3.txt")
    'e:\\python'
    
    os.path.basename(path)
    返回path中的文件名,就是os.path.split(path)的第二个元素
    >>> os.path.basename("e:\\python\\3.txt")
    '3.txt'
    
    os.path.exists(path)
    判断path指定的目录或文件是否存在
    
    >>> os.path.exists("e:\\python")
    True
    >>> os.path.exists("e:\\python\\3.txt")
    True
    
    os.path.isabs(path)
    判断path是否是绝对路径
    >>> os.path.isabs("e:\\python\\3.txt")
    True
    os.path.isfile(path)
    判断path是否是文件
    >>> os.path.isfile("e:\\python\\3.txt")
    True
    >>> os.path.isfile("e:\\python")
    False
    
    os.path.isdir(path)
    判断path是否目录
    
    >>> os.path.isdir("e:\\python\\3.txt")
    False
    >>> os.path.isdir("e:\\python")
    True
    
    os.path.normpath(path)
    将path转换为规范的路径
    >>> os.path.normpath("e:/python\3.txt")
    'e:\\python\x03.txt'
    
    os.path.join(a, *p)
    连接两个或更多的路径名,中间以“\”分隔,如果所给的参数中都是绝对路径名,那先给的绝对路径将会被丢弃
    >>> os.path.join("e:\\python","test","3.txt")
    'e:\\python\\test\\3.txt'
    
    #两个都是绝对路径e:\\python被丢弃
    >>> os.path.join("e:\\python","e:\\test")
    'e:\\test'
    
    os.path.getatime(filename)
    返回文件的最后访问时间,返回时间戳
    
    >>> os.path.getatime("e:\\python\\3.txt")
    1534340733.0906618
    
    #coding=utf-8
    import os
    import time
    #获取文件最后访问时间
    lastTime = os.path.getatime(r"e:\python\a.py")
    print (lastTime)
    #将时间戳转成时间元组
    formatTime = time.localtime(lastTime)
    print (formatTime)
    #格式化时间元组为时间字符串
    print (time.strftime("%Y-%m-%d %H:%M:%S",  formatTime))
    
    os.path.getctime(filename)
    返回文件的创建时间,返回时间戳
    
    >>> os.path.getctime("e:\\python\\3.txt")
    1534340733.0906618
    
    os.path.getmtime(filename)
    返回文件的最后修改时间,返回时间戳
    
    >>> os.path.getmtime("e:\\python\\3.txt")
    1539184690.78357
    os.environ
    获取当前的环境变量
    >>> os.environ
    
    environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\Ad
    )': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\
    : 'NO', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\Administrator.USER-2018
    ER_OF_PROCESSORS': '4', 'OS': 'Windows_NT', 'PATH': 'd:\\ProgramData\\An
    nda3\\Library\\bin;d:\\ProgramData\\Anaconda3\\Scripts;C:\\Python36\\Scr
    \Library\\usr\\bin;d:\\ProgramData\\Anaconda2\\Library\\bin;d:\\ProgramD
    v1.0\\;C:\\Python27;C:\\Python27\\Scripts', 'PATHEXT': '.COM;.EXE;.BAT;.
    del 37 Stepping 5, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REV
    x86)', 'PROGRAMW6432': 'C:\\Program Files', 'PROMPT': '$P$G', 'PSMODULEP
    TEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\Windows', 'TEMP': 'C:\\Users\\ADMINI
    E': 'Administrator', 'USERPROFILE': 'C:\\Users\\Administrator.USER-20180
    ilogfile.log', '_DFX_INSTALL_UNSIGNED_DRIVER': '1'})

关键字