zoukankan      html  css  js  c++  java
  • python os模块中的文件操作

    os模块中的文件操作:
    os 模块属性
    linesep 用于在文件中分隔行的字符串
    sep 用来分隔文件路径名的字符串
    pathsep 用于分隔文件路径的字符串
    curdir 当前工作目录的字符串名称
    pardir (当前工作目录的)父目录字符串名称
    1.重命名:os.rename(old, new)
    2.删除:os.remove(file)
    3.列出目录下的文件:os.listdir(path)
    4.获取当前工作目录:os.getcwd()
    5.改变工作目录:os.chdir(newdir)
    6.创建多级目录:os.makedirs(r"c:\python\test")
    7.创建单个目录:os.mkdir("test")
    8.删除多个目录:os.removedirs(r"c:\python") #删除所给路径最后一个目录下所有空目录。
    9.删除单个目录:os.rmdir("test")
    10.获取文件属性:os.stat(file)
    11.修改文件权限与时间戳:os.chmod(file)
    12.执行操作系统命令:os.system("dir")
    13.启动新进程:os.exec(), os.execvp()
    14.在后台执行程序:osspawnv()
    15.终止当前进程:os.exit(), os._exit()
    16.分离文件名:os.path.split(r"c:\python\hello.py") --> ("c:\\python", "hello.py")
    17.分离扩展名:os.path.splitext(r"c:\python\hello.py") --> ("c:\\python\\hello", ".py")
    18.获取路径名:os.path.dirname(r"c:\python\hello.py") --> "c:\\python"
    19.获取文件名:os.path.basename(r"r:\python\hello.py") --> "hello.py"
    20.判断文件是否存在:os.path.exists(r"c:\python\hello.py") --> True
    21.判断是否是绝对路径:os.path.isabs(r".\python\") --> False
    22.判断是否是目录:os.path.isdir(r"c:\python") --> True
    23.判断是否是文件:os.path.isfile(r"c:\python\hello.py") --> True
    24.判断是否是链接文件:os.path.islink(r"c:\python\hello.py") --> False
    25.获取文件大小:os.path.getsize(filename)
    26.*******:os.ismount("c:\\") --> True
    27.搜索目录下的所有文件:os.path.walk()

    详细出处参考:http://www.jb51.net/article/28905.htm

  • 相关阅读:
    sql server delete语句
    sql server 通配符
    sql server join联结
    windows下如何使用两个版本的nodejs
    PHP的Trait 特性
    PHP错误与异常处理try和catch
    laravel belongsTo()&hasOne()用法详解
    Laravel Model 的 fillable (白名单)与 guarded (黑名单)
    js原生,缓动动画封装
    js原生轮播图
  • 原文地址:https://www.cnblogs.com/gisoracle/p/2523922.html
Copyright © 2011-2022 走看看