2、写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作
 1 # 方法一
 2 # import os
 3 # def fun(): #y为要修改的内容,z为修改的结果
 4 #     y=input("请输入你要修改的内容:>>>")
 5 #     z=input("请输入你想要修改后的内容:>>>")
 6 #     with open('a.txt','r',encoding='utf-8') as read_f,\
 7 #     open('b.txt','w',encoding='utf-8') as write_f:
 8 #         data=read_f.read()
 9 #         write_f.write(data.replace(y,z))
10 #     os.remove('a.txt')
11 #     os.rename('b.txt','a.txt')
12 # fun()
13 
14 # 方法二
15 # import os
16 # def fun(old_content,new_content): #y为要修改的内容,z为修改的结果
17 #     with open('a.txt','r',encoding='utf-8') as read_f,\
18 #     open('b.txt','w',encoding='utf-8') as write_f:
19 #         for line in read_f:
20 #             if old_content in line:
21 #                 write_f.write(line.replace(old_content, new_content))
22 #     os.remove('a.txt')
23 #     os.rename('b.txt','a.txt')
24 # fun('alex','xx')
# 3、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。
 1 def check(o):
 2    if o:#就相当于bool(o)==True, #判断o的布尔值,如果不为空就执行子代码块的内容
 3        if type(o) is str:
 4            for i in o:
 5                if i==' ':
 6                    return True
 7        else:
 8            for i in o:
 9                if not i :
10                    return True
11    else:
12        return True
13 print(check('fh fh '))
14 print(check(['',11,22,' fdg ']))
View Code

相关文章: