动动手:
0.编写一个程序,接受用户的输入并保存为新的文件,程序实现如图:
实现代码:
1 def new_file(file_name): 2 f = open (file_name , "w") 3 print("请输入内容[单独输入':w'保存退出]:") 4 while True: 5 write_some = input() 6 if write_some != ':w': 7 f.write("%s " % write_some) 8 else: 9 break 10 f.close() 11 file_name = input("请输入文件名:") 12 new_file(file_name)
1.编写一个程序,比较用户输入的两个文件,如果不同,显示出所有不同处的行号与第一个不同字符的位置,程序实现如图:
实现代码:
1 # 同一文件夹内比较 2 def file_compare(file1,file2): 3 f1 = open(file1,"r") 4 f2 = open(file2,"r") 5 count = 0 6 differ = [] 7 8 for each_line1 in f1: 9 each_line2 = f2.readline() 10 count += 1 11 if each_line1 != each_line2: 12 differ.append(count) 13 14 f1.close() 15 f2.close() 16 return differ 17 18 file1 = input("请输入需要比较的头一个文件名:") 19 file2 = input("请输入需要比较的后一个文件名:") 20 21 differ = file_compare(file1,file2) 22 if len(differ) == 0: 23 print("两个文件完全一样。") 24 else: 25 print("两个文件有%d处不同" % len(differ) ) 26 for each in differ: 27 print("第%d行不一样" % each)
2.编写一个程序,当用户输入文件名和行数(N)后,将该文件的前N行内容打印在屏幕上,程序实现如图:
实现代码:
1 def read_file(file,num): 2 f = open(file) 3 each_line_length = len(file) 4 if num == 0: 5 print(" ") 6 if each_line_length <= num: 7 print("文件行数只有%d行,已全部读取"% each_line_length) 8 for each_line in f : 9 print(each_line) 10 else: 11 # 读取前多少行 12 for i in range(num): 13 print(f.readline(),end = "") 14 15 f.close() 16 17 file = input("请输入要打开的文件名(带路径):") 18 num = int (input("请输入需要显示改文件前几行:")) 19 read_file(file,num)
方法2:
1 def file_view(file_name,file_num): 2 print(" 文件%s的前%s的内容如下: " % (file_name,file_num)) 3 f = open(file_name) 4 for i in range(int(file_num)): 5 print(f.readline(),end = " ") 6 f.close() 7 file_name = input("请输入要打开的文件名(带路径):") 8 file_num = (input("请输入需要显示改文件前几行:")) 9 file_view(file_name,file_num)
3.在上一题的基础上进行扩展,用户可以随意输入需要显示的行数(如果输入13:21打印第13行到21行,输入为:21打印前21行,输入21:则打印从第21行开始到文件结尾所有内容)
实现代码:
1 def read_file(file,num1): 2 f = open(file) 3 list1 = list(num1.split(":")) 4 # print(list1) 5 begin = list1[0] 6 end = list1[1] 7 if begin == "": 8 begin = 1 9 if end =="": 10 end = -1 11 if begin ==1 and end == -1: 12 temp = "全文" 13 # 显示全文 14 # print(f.readline()) 15 elif begin ==1: 16 temp = "从开始到%d行" % end 17 elif end == -1: 18 temp = "从%d行到最后" % begin 19 else: 20 temp = "从%s到%s行" % (begin,end) 21 print("文件%s" % file +temp+"的内容如下:" ) 22 23 begin = int(begin)-1 24 end = int (end ) 25 lines = end-begin 26 each_line_length = len(file) 27 # 消耗掉。不显示内容 28 for i in range(begin): 29 f.readline() 30 if lines <0: 31 print(f.read()) 32 else: 33 for j in range(lines): 34 print(f.readline(),end= "") 35 f.close() 36 37 file = input("请输入要打开的文件名(带路径):") 38 num1 = input("请输入需要显示该文件行数【格式如13:21或:21或13:或:】:") 39 read_file(file,num1)
4.编写一个程序,实现“全部替换”功能,程序实现如图:
实现代码:
1 def file_replace(file_name,old_word,new_word): 2 f = open(file_name) 3 count= 0 4 content = [] 5 num =0 6 for eachline in f : 7 num += 1 8 if old_word in eachline: 9 count += eachline.count(old_word) 10 eachline = eachline.replace(old_word, new_word) 11 content.append(eachline) 12 else: 13 content.append(eachline) 14 print("文件中共有"+str(count)+"个"+old_word) 15 prompt = input("您确定要把所有的"+old_word+"替换成"+new_word+"吗?【YES/NO】") 16 if prompt in ['YES','yes','Yes']: 17 f_write = open(file_name,"w") 18 for i in range(len(content)): 19 temp = str(content[i]) 20 f_write.writelines(temp) 21 f_write.close() 22 f.close() 23 24 file_name = input("请输入文件名:") 25 old_word = input("请输入需要替换的单词和字符:") 26 new_word = input("请输入新的单词或者字符:") 27 file_replace(file_name,old_word,new_word)
函数变量可多个!!