zoukankan      html  css  js  c++  java
  • os常用方法

    os.getcwd() #返回当前工作目录
    os.chdir(path) #改变工作目录
    os.listdir(path=dir_path) #列举指定目录中的文件名
    os.mkdir(path) #创建建单层目录,如果该目录已存在则抛出异常
    os.makedirs(path) #递归创建多层目录,如该目录已存在抛出异常
    os.remove(path) #删除文件
    os.rmdir(path) #删除单层目录,如该目录非空则抛出异常
    os.removedirs(path) #递归删除目录,从子目录到父目录逐层尝试删除,遇到目录非空则抛出异常
    os.rename(old,new) #将文件old重命名为new
    os.system(command) #运行系统shell命令
    os.walk(top) #遍历top路径以下所有子目录,返回一个三元组:(路径,[包含目录],[包含文件])
    os.curdir #属性,表示当前目录
    os.pardir #属性,表示上一级目录
    os.sep #属性,输出操作系统特定的路径分隔符(win下为'\',Linux下为'/')
    os.linesep #属性,当前平台使用的行终止符(Win下为‘
    ’,Linux下为'
    ')
    os.name #属性,指待当前使用的操作系统
    
    路径方法
    os.path.basename(path) #去掉目录路径,单独返回文件名
    os.path.dirname(path) #去掉文件名,单独返回目录路径
    os.path.join(path1[,],path2[,...]) #将path1,path2各部分组成一个路径名
    os.path.split(path) #分割文件名和路径,返回一个(f_path,f_name)元组,
      #  如果完全使用目录,它也会将最后一个目录作为文件名分离
    os.path.splitext(path) #分离文件名和后缀名,返回(f_name,f_extension)元组,
      #  如果完全使用目录,它也会将最后一个目录作为文件名分离
    os.path.getsize(file) #返回指定文件的尺寸,单位是字节
    os.path.getatime(file) #返回指定文件最近的访问时间
      #(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
    os.path.getctime(file) #返回指定文件的创建时间
    os.path.getmtime(file) #返回指定文件最新的修改时间
    os.path.exists(path) #判断指定路径是否存在(目录或者文件)
    os.path.isabs(path) #判断是否为绝对路径
    os.path.isdir(path) #判断指定路径是否存在且是一个目录
    os.path.isfile(path) #判断指定路径是否存在且是一个文件
    os.path.islink(path) #判断指定路径是否存在且是一个符号链接
    os.path.ismount(path) #判断指定路径是否存在且是一个挂载点
    os.path.samefile(path1,path2) #判断path1,path2是否指向同一个文件
    
  • 相关阅读:
    Java-对象数组排序
    aoj 0118 Property Distribution
    poj 3009 Curling 2.0
    poj 1979 Red and Black
    AtCoder Regular Contest E
    AtCoder Beginner Contest 102
    AtCoder Beginner Contest 104
    SoundHound Inc. Programming Contest 2018
    poj 3233 Matrix Power Series
    poj 3734 Blocks
  • 原文地址:https://www.cnblogs.com/he-qing-qing/p/12783730.html
Copyright © 2011-2022 走看看