Python實(shí)現(xiàn)替換文件中指定內(nèi)容的方法
本文實(shí)例講述了Python實(shí)現(xiàn)替換文件中指定內(nèi)容的方法。分享給大家供大家參考,具體如下:
這里使用python編寫的程序,實(shí)現(xiàn)如下功能:將文件中的指定子串 修改為 另外的子串
編寫的python程序,文件名是file_replace.py,具體代碼如下:
#!/usr/bin/env python #_*_ coding:utf-8 _*_ import sys,os if len(sys.argv)<4 or len(sys.argv)>5: sys.exit('There needs four or five parameters') elif len(sys.argv)==4: print 'usage:./file_replace.py old_text new_text filename' else: print 'usage:./file_replace.py old_text new_text filename --bak' old_text,new_text=sys.argv[1],sys.argv[2] file_name=sys.argv[3] f=file(file_name,'rb') new_file=file('.%s.bak' % file_name,'wb')#文件名以.開頭的文件是隱藏文件 for line in f.xreadlines():#f.xreadlines()返回一個(gè)文件迭代器,每次只從文件(硬盤)中讀一行 new_file.write(line.replace(old_text,new_text)) f.close() new_file.close() if '--bak' in sys.argv: #'--bak'表示要求對(duì)原文件備份 os.rename(file_name,'%s.bak' % file_name) #unchanged os.rename('.%s.bak' % file_name,file_name) #changed else: os.rename(file_name,'wahaha.txt')#此處也可以將原文件刪除,以便下一語句能夠正常執(zhí)行 os.rename('.%s.bak' % file_name,file_name)
下面是代碼執(zhí)行的一個(gè)例子:
song@ubuntu:~$ more hello.txt Hello python Hello world python Hello world Hello song@ubuntu:~$ python file_replace.py Hello love hello.txt --bak usage:./file_replace.py old_text new_text filename --bak song@ubuntu:~$ ls Desktop Documents file_replace.py Music systemExit.py diff1.txt Downloads hello.txt Pictures Templates diff.txt examples.desktop hello.txt.bak Public Videos song@ubuntu:~$ more hello.txt love python love world python love world love song@ubuntu:~$ more hello.txt.bak Hello python Hello world python Hello world Hello song@ubuntu:~$
更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python編寫電話薄實(shí)現(xiàn)增刪改查功能
這篇文章主要為大家詳細(xì)介紹了Python編寫電話薄實(shí)現(xiàn)增刪改查功能的相關(guān)資料,感興趣的朋友可以參考一下2016-05-05詳解Python進(jìn)行數(shù)據(jù)相關(guān)性分析的三種方式
相關(guān)系數(shù)量化數(shù)據(jù)集的變量或特征之間的關(guān)聯(lián)。這些統(tǒng)計(jì)數(shù)據(jù)對(duì)科學(xué)和技術(shù)非常重要,Python?有很好的工具可以用來計(jì)算它們。SciPy、NumPy?和Pandas相關(guān)方法以及數(shù)據(jù)可視化功能,感興趣的可以了解一下2022-04-04跟老齊學(xué)Python之通過Python連接數(shù)據(jù)庫
現(xiàn)在在做python的時(shí)候需要用到數(shù)據(jù)庫,于是自己重新整理了一下數(shù)據(jù)庫的知識(shí),并且熟悉了python中MysqlDB模塊的功能和函數(shù)等接口,現(xiàn)在系統(tǒng)地來總結(jié)一下吧2014-10-10基于pygame實(shí)現(xiàn)童年掌機(jī)打磚塊游戲
這篇文章主要為大家詳細(xì)介紹了基于pygame實(shí)現(xiàn)童年掌機(jī)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02Python導(dǎo)入父文件夾中模塊并讀取當(dāng)前文件夾內(nèi)的資源
這篇文章主要給大家介紹了關(guān)于Python導(dǎo)入父文件夾中模塊并讀取當(dāng)前文件夾內(nèi)資源的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11python3實(shí)現(xiàn)點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)點(diǎn)餐系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01YOLOv5車牌識(shí)別實(shí)戰(zhàn)教程(六)性能優(yōu)化與部署
這篇文章主要介紹了YOLOv5車牌識(shí)別實(shí)戰(zhàn)教程(六)性能優(yōu)化與部署,在這個(gè)教程中,我們將一步步教你如何使用YOLOv5進(jìn)行車牌識(shí)別,幫助你快速掌握YOLOv5車牌識(shí)別技能,需要的朋友可以參考下2023-04-04