zoukankan      html  css  js  c++  java
  • python os模块

     对目录的操作:方便在不通的电脑上的操作。这个需要把他倒入

    import os

    print dir(os)

    print type(help(os))

    os获取上层目录的另一种方法:os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))      os.pardir是“..”

    os.chdir(path)   切换到哪个目录下,相当于linux的 cd path

     os其他的一些常用方法

    os.system('ping -t www.baidu.com')   # 里面的是命令,相当于在doc窗口自己敲的命令

    os.mkdir("c:/log")     #创建文件夹


    #!/usr/bin/env/python
    # -*-coding:utf-8-*-

    import os
    #1.os.path.abspath(path) 返回path规范化的绝对路径。
    print "1返回path规范化的绝对路径。",os.path.abspath("exceldemo1.xlsx")
    print "1返回path规范化的绝对路径。",os.path.abspath("../excel/exceldemo1.xlsx")

    #2 os.path.split(path) 将path分割成目录和文件名二元组返回
    print "2将path分割成目录和文件名二元组返回。",os.path.split("/Users/newcomer/PycharmProjects/error/wuya/excel/exceldemo1.xlsx")

    #3.os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素。
    print "3返回path的目录",os.path.dirname("/Users/newcomer/PycharmProjects/error/wuya/excel/exceldemo1.xlsx")

    #4 os.path.basename(path) 返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素。
    print "4返回path最后的文件名",os.path.basename("/Users/newcomer/PycharmProjects/error/wuya/excel/exceldemo1.xlsx")

    #5.os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False。
    print "5如果path存在,返回True;如果path不存在,返回False。",os.path.exists("/Users/newcomer/PycharmProjects/error/wuya/excel/")

    #6 os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False。
    print "6如果path是一个存在的文件,返回True。否则返回False。",os.path.isfile("exceldemo1.xlsx")

    #7.os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False。
    print "7如果path是一个存在的目录,则返回True。否则返回False",os.path.isdir("../excel/")

    #8.os.path.getsize(path) 返回path的文件的大小(字节)
    print "8返回path的文件的大小(字节)",os.path.getsize("exceldemo1.xlsx")

    #9.os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间。
    print "9返回path所指向的文件或者目录的最后存取时间",os.path.getatime("exceldemo1.xlsx")

    #10.os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
    print "10返回path所指向的文件或者目录的最后修改时间",os.path.getmtime("exceldemo1.xlsx")

    
    
  • 相关阅读:
    MyEclipse 中 添加 js自动完成模版
    jQuery css,position,offset,scrollTop,scrollLeft用法
    eclipse中报错:Errors running builder “Integrated External Tool Builder” on project
    jQuery children等筛选用法
    jQuery html text val方法使用
    EventBus在Android中的简单使用
    mvc 中Range中max和min值晚绑定
    <转> Libvirt学习总结
    hdu 4409 Family Name List(LCA&有坑点)
    Mybatis 入门之resultMap与resultType解说实例
  • 原文地址:https://www.cnblogs.com/peiminer/p/9150515.html
Copyright © 2011-2022 走看看