我就在这里
分类: Python/Ruby
2013-01-16 20:38:38
[root@vps616 python]# cat main.py #python2.7.3 #coding=utf8 import os import sys def sub_directory(path, old, new): path_set = [] for dirpath, dirnames, filenames in os.walk(path): for name in filenames: full_path = os.path.join(dirpath, name) full_path = full_path.replace(old, new) path_set.append(full_path) return path_set if __name__ == "__main__": print sub_directory(sys.argv[1], sys.argv[2], sys.argv[3])
import os properties = open("text.txt",'rb+') lines = properties.readlines() d="" for line in lines: c = line.replace("my name is abc", "my name is efg") d += c properties.seek(0) #不要让python记住执行到这里,从文件头还始 properties.truncate() #清空文件 properties.write(d) properties.close()