題目為
將目錄下副檔名為net的檔案 讀取內容
內容 在分號;以前的描述 前後要追加上引號'
做法如下
#! python3
import os
idFilter = ';'
insertchr = '\''
ExtName = 'net'
for dirname, dirnames, filenames in os.walk('.'):
# print path to all filenames.
for filename in filenames:
if ExtName in filename and 'ICL' not in filename:
print(filename)
fin = open(filename, 'r', encoding = 'UTF-8')
fout = open('ICL_'+filename, 'w', encoding = 'UTF-8')
while True :
fileaString = fin.readline()
if fileaString =='':
fin.close()
fout.close()
break
if idFilter not in fileaString:
fout.write(fileaString)
if idFilter in fileaString:
idPosition = fileaString.find(idFilter)
print(idPosition)
fileaString2 = insertchr + fileaString[:idPosition-1] + insertchr + fileaString[idPosition-1:]
fout.write(fileaString2)