python os模块进行文件的删除与重命名
2015-02-28 10:08
435 查看
阅读Python 标准库中的代码格式不对,运行完成,进行保存。
#coding:utf-8 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 ="test.txt" replace(file,"hello", "tjena2") replace(file,"tjena", "hello2")
相关文章推荐
- Python----os 模块对文件进行操作
- python有关文件夹与文件的查找,删除等功能 在 os 模块中实现
- Python os.path和shutil模块实现文件复制、删除
- 批量重命名文件os模块小练习Python3
- python的PEXPECT模块进行交换机配置文件的自动备份
- 利用python自带的os模块删除windows机器的模块
- (原创)Python文件与文件系统系列(2)——os模块对文件、文件系统操作的支持
- Python os 模块文件操作
- os 模块 python file 与文件路径
- python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块
- python中对文件、文件夹的操作 os模块
- Python3之OS模块文件操作
- python os模块,平常操作文件方便,执行cmd命令
- python 删除某个路径下所有文件及模块调用注意事项
- python对指定目录下文件进行批量重命名的方法
- Python模块整理(一):文件系统相关模块os
- python文件读写,python文件os模块
- Python OS 文件操作模块常用函数
- 【Python】Python的urllib模块、urllib2模块进行网页下载文件
- python 目录文件操作 os模块