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")


原文地址:https://www.cnblogs.com/peiminer/p/9150515.html