#/usr/bin/env python#yehui
import os, sys
'''
try: os.mkdir('userinfo')except FileExistsError: print("文件夹已存在跳过")
'''
####判断对应目录是否存在
class PathJudge:
def __init__(self):self.path1 = "userinfo" self.path2 = "user_dict_info" def judge_path(self):if os.path.exists('userinfo'):print("file %s is exists and then pass" % self.path1)else: os.mkdir(self.path1)if os.path.exists('user_dict_info'):print("file %s is exists and then pass" % self.path2)else: os.mkdir(self.path2)class Sed:
def __init__(self, oldstr, newstr): ###定义替换sedself.old_file = oldstr ###旧内容self.new_file = newstr ###新内容self.file_path = "userinfo/clock" ###创建目录变量 def sedfile(self): with open(self.file_path, 'r') as self.f, open("userinfo/back", "a+") as self.f1: ##同时打需要修改的文件,及新创建新内容的文件for self.i in self.f:if self.old_file in self.i:self.i = self.i.replace(self.old_file, self.new_file) ##对内容进行替换操作self.f1.write(self.i) ##写入操作self.f1.flush() ##立即刷新 os.remove(self.file_path) ##删除原文件 os.rename("userinfo/back", self.file_path) ##新文件改名
if __name__ == '__main__':
sed = Sed("0这是坚强的肥DD!", "我去!!!") sed.sedfile()
转载于:https://www.cnblogs.com/yehui/articles/9042141.html