os.path等os模块函数

os.path.abspath(path) #返回绝对路径
os.path.basename(path) #返回文件名
os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。
os.path.dirname(path) #返回文件路径
os.path.exists(path)  #路径存在则返回True,路径损坏返回False
os.path.lexists  #路径存在则返回True,路径损坏也返回True
os.path.expanduser(path)  #把path中包含的"~"和"~user"转换成用户目录
os.path.expandvars(path)  #根据环境变量的值替换path中包含的”$name”和”${name}”
os.path.getatime(path)  #返回最后一次进入此path的时间。
os.path.getmtime(path)  #返回在此path下最后一次修改的时间。
os.path.getctime(path)  #返回path的大小
os.path.getsize(path)  #返回文件大小,如果文件不存在就返回错误
os.path.isabs(path)  #判断是否为绝对路径
os.path.isfile(path)  #判断路径是否为文件
os.path.isdir(path)  #判断路径是否为目录
os.path.islink(path)  #判断路径是否为链接
os.path.ismount(path)  #判断路径是否为挂载点()
os.path.join(path1[, path2[, ...]])  #把目录和文件名合成一个路径
os.path.normcase(path)  #转换path的大小写和斜杠
os.path.normpath(path)  #规范path字符串形式
os.path.realpath(path)  #返回path的真实路径
os.path.relpath(path[, start])  #从start开始计算相对路径
os.path.samefile(path1, path2)  #判断目录或文件是否相同
os.path.sameopenfile(fp1, fp2)  #判断fp1和fp2是否指向同一文件
os.path.samestat(stat1, stat2)  #判断stat tuple stat1和stat2是否指向同一个文件
os.path.split(path)  #把路径分割成dirname和basename,返回一个元组
os.path.splitdrive(path)   #一般用在windows下,返回驱动器名和路径组成的元组
os.path.splitext(path)  #分割路径,返回路径名和文件扩展名的元组
os.path.splitunc(path)  #把路径分割为加载点与文件
os.path.walk(path, visit, arg)  #遍历path,进入每个目录都调用visit函数,visit函数必须有
3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有
文件名,args则为walk的第三个参数
os.path.supports_unicode_filenames  #设置是否支持unicode路径名

os.pardir  #当前目录的父目录

os.path.join(script_dir, os.pardir, os.pardir)  #script_dir的祖父目录,例如:script_dir是F:123,那么os.path.join(script_dir, os.pardir, os.pardir)是F:123....

os.path.abspath(os.path.join(script_dir, os.pardir, os.pardir)) #script_dir的祖父目录,例如:script_dir是F:123,那么os.path.abspath(os.path.join(script_dir, os.pardir, os.pardir))是F:1

os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作

   

1.文件操作

build-in函数 open 实现文件创建, 打开, 修改文件的操作

import os

import string

   

def replace(file, search_for, replace_with):

# replace strings in a text file

   

back = os.path.splitext(file)[0] + ".bak"

temp = os.path.splitext(file)[0] + ".tmp"

   

try:

# remove old temp file, if any

os.remove(temp)

except os.error:

pass

   

fi = open(file) #

fo = open(temp, "w")        #

   

for s in fi.readlines():

fo.write(string.replace(s, search_for, replace_with))

   

fi.close()

fo.close()

   

try:

# remove old backup file, if any

os.remove(back)

except os.error:

pass

   

# rename original to backup...

os.rename(file, back)

   

# ...and temporary to original

os.rename(temp, file)

   

# try it out!

   

file = "c:samplessample.txt"

   

replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena

replace(file, "tjena", "hello")

   

2. 目录操作

os 模块包含了许多对目录操作的函数

listdir 函数返回给定目录下的所有文件(包括目录)

   

import os

for file in os.listdir("c:qtest"):

print file

   

getdir 获取当前目录

chdir 改变当前路径

   

cwd = os.getcwd()

print "1", cwd

# go down

os.chdir("c:qtest")

print "2", os.getcwd()

# go back up

os.chdir(os.pardir)#返回当前目录的父目录

print "3", os.getcwd()

   

makedirs removedirs 生成和删除目录

makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除

   

import os

os.makedirs("c:\test\multiple\levels")

fp = open("c:\test\multiple\levels\file.txt", "w")

fp.write("inspector praline")

fp.close()

# remove the file

os.remove("c:\test\multiple\levels\file.txt")

# and all empty directories above it

os.removedirs("c:\test\multiple\levels")

   

mkdir 和 rmdir只能处理单级目录操作.

若要删除非空目录, 可使用 shutil模块中的rmtree函数

   

3. 文件属性的操作

import os

import time

file = 'c:qtesteditor.pyc'

   

st = os.stat(file)

print "state", file

   

def dump(st):

mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st

print "- size:", size, "bytes"

print "- owner:", uid, gid

print "- created:", time.ctime(ctime)

print "- last accessed:", time.ctime(atime)

print "- last modified:", time.ctime(mtime)

print "- mode:", oct(mode)

print "- inode/dev:", ino, dev

   

print dir(st)

print        

dump(st)

# print

   

fp = open(file)

st = os.fstat(fp.fileno())

print "fstat", file

dump(st)

原文地址:https://www.cnblogs.com/smuxiaolei/p/7617484.html