python文件操作的二个例子。
例1,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])
例2,python文件操作。
 
复制代码 代码示例:
#!/bin/python
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()
您可能感兴趣的文章:
python文件操作技巧总结
通过实例学习python文件操作
python目录与文件操作