Python OS模块学习(一)

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:\samples\sample.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:\qtest\editor.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)

   

remark: os.stat(path/file)返回文件的对应属性值st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user id of owner), st_gid (group id of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows):

os.fstat(path/file)

Return status for file descriptor fd, like stat().


转载于:https://my.oschina.net/XoAWFJYXq5/blog/666239

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值