




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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: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:testmultiplelevels) fp = open(c:testmultiplelevelsfile.txt, w) fp.write(inspector praline) fp.close() # remove the file os.remove(c:testmultiplelevelsfile.txt) # and all empty directories above it os.removedirs(c:testmultiplelevels) 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) 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(). 4.进程的相关处理 system( )给当前进程输入系统shell命令 import osif = nt: command = direlse: command = ls -l os.system(command) execvp 开始一个新进程, 以取代目前进程 import osimport sysprogram = pythonarguments = hello.pyprint os.execvp(program, (program,) + tuple(arguments)print goodbye 在windows 平台下用spawn( )等函数执行新进程 import osimport stringdef run(program, *args):# find executablefor path in string.split(os.environPATH, os.pathsep): file = os.path.join(path, progr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 废玻璃回收再生回收项目可行性研究报告
- 2025-2026学年人教版(2024)小学数学二年级上册期中(第1-3单元)核心知识点归纳
- 有机硅耐热漆项目可行性研究报告
- 防汛知识培训宣传
- 装饰施工工装合同
- DB65T 4119-2018 绵羊细管冷冻精液操作规程
- 消费优惠市场分析研究
- 星际尘埃对空间材料性能的影响-洞察及研究
- 窗帘制作安装合同正式版范本2篇
- 培训班租赁合同3篇
- GB/T 46150.2-2025锅炉和压力容器第2部分:GB/T 46150.1的符合性检查程序要求
- 第1课 假期有收获 第1课时(课件)2025-2026学年道德与法治二年级上册统编版
- 《人为因素与航空法规》课件(共九章)
- (正式版)HGT 20593-2024 钢制化工设备焊接与检验工程技术规范
- GB/T 31586.1-2015防护涂料体系对钢结构的防腐蚀保护涂层附着力/内聚力(破坏强度)的评定和验收准则第1部分:拉开法试验
- 高压燃气管线施工安全生产措施
- 医院临床科主任考核测评表
- 大国工匠精神PPT课件
- 中交二公局大西铁路大荔特大桥项目部拌合站管理制度汇编
- 古今数学思想读书笔记
- (完整版)施工组织设计中临时用电计算
评论
0/150
提交评论