程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python reads the txt file, finds the corresponding content, and generates a new file

編輯:Python
def modifyip(tfile, deviceAddress): # tfile For the target file ,deviceAddress For the criteria to find 
desktop_path = 'C:/Users/JiajunHu/Desktop/' # The newly created txt File storage path 
full_path = desktop_path + deviceAddress + '.txt' # With deviceAddress For the file name 
try:
lines = open(tfile, 'r', encoding='utf-8').readlines()
flen = len(lines) - 1
print(flen)
for i in range(flen):
if deviceAddress in lines[i]:
file = open(full_path, 'a')
file.write(lines[i - 5] + "\n" +lines[i - 4] + "\n" +
lines[i - 3]+ "\n" + lines[i] + "\n" + lines[i + 4])
except Exception as e:
print(e)
if __name__ == '__main__':
modifyip('C:/Users/JiajunHu/Desktop/log_4.txt',
'8akDIW5BSG0U') # stay 'C:/Users/JiajunHu/Desktop/log_4.txt' Search for '8akDIW5BSG0U'

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved