在下面的代碼我試圖以取代中有以下內容的文件的內容。 hellohello world
和hellohello應你好更換並寫回file.Ho去這個蟒蛇搜索和寫入文件
#!/usr/bin/python
import os
new_file_list=[]
all_files=os.listdir("/tmp")
for ff in all_files:
if ff.endswith(".txt"):
new_file_list.append(ff)
for files in new_file_list:
if files == "a.txt":
print "======================================="
file_name="/tmp/"+str(files)
print file_name
f=open(file_name ,"rw")
while True:
print "======================================="
for line in f.readline():
print line
print "======================================="
f.write(line.replace("hellohello","hello"))
print line
else:
break
for line in f.readline():
print line
f.close()
謝謝!錯過了 – 2012-02-16 09:53:12