当前位置:网站首页>第029讲:文件:一个任务 | 课后测试题及答案
第029讲:文件:一个任务 | 课后测试题及答案
2022-06-22 20:05:00 【ChaseTimLee】
动动手:
0. 编写一个程序,接受用户的输入并保存为新的文件,程序实现如图:

def file_write(file_name):
f = open(file_name, 'w')
print('请输入内容【单独输入\':w\'保存退出】:')
while True:
write_some = input()
if write_some != ':w':
f.write('%s\n' % write_some)
else:
break
f.close()
file_name = input('请输入文件名:')
file_write(file_name)
1. 编写一个程序,比较用户输入的两个文件,如果不同,显示出所有不同处的行号与第一个不同字符的位置,程序实现如图:

def file_compare(file1, file2):
f1 = open(file1)
f2 = open(file2)
count = 0 # 统计行数
differ = [] # 统计不一样的数量
for line1 in f1:
line2 = f2.readline()
count += 1
if line1 != line2:
differ.append(count)
f1.close()
f2.close()
return differ
file1 = input('请输入需要比较的头一个文件名:')
file2 = input('请输入需要比较的另一个文件名:')
differ = file_compare(file1, file2)
if len(differ) == 0:
print('两个文件完全一样!')
else:
print('两个文件共有【%d】处不同:' % len(differ))
for each in differ:
print('第 %d 行不一样' % each)
2. 编写一个程序,当用户输入文件名和行数(N)后,将该文件的前N行内容打印到屏幕上,程序实现如图:

def file_view(file_name, line_num):
print('\n文件%s的前%s的内容如下:\n' % (file_name, line_num))
f = open(file_name)
for i in range(int(line_num)):
print(f.readline(), end= '')
f.close()
file_name = input(r'请输入要打开的文件(C:\\test.txt):')
line_num = input('请输入需要显示该文件前几行:')
file_view(file_name, line_num)
3. 呃,不得不说我们的用户变得越来越刁钻了。要求在上一题的基础上扩展,用户可以随意输入需要显示的行数。(如输入13:21打印第13行到第21行,输入:21打印前21行,输入21:则打印从第21行开始到文件结尾所有内容)

def file_view(file_name, line_num):
if line_num.strip() == ':':
begin = '1'
end = '-1'
(begin, end) = line_num.split(':')
if begin == '':
begin = '1'
if end == '':
end = '-1'
if begin == '1' and end == '-1':
prompt = '的全文'
elif begin == '1':
prompt = '从开始到%s' % end
elif end == '-1':
prompt = '从%s到结束' % begin
else:
prompt = '从第%s行到第%s行' % (begin, end)
print('\n文件%s%s的内容如下:\n' % (file_name, prompt))
begin = int(begin) - 1
end = int(end)
lines = end - begin
f = open(file_name)
for i in range(begin): # 用于消耗掉begin之前的内容
f.readline()
if lines < 0:
print(f.read())
else:
for j in range(lines):
print(f.readline(), end='')
f.close()
file_name = input(r'请输入要打开的文件(C:\\test.txt):')
line_num = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】:')
file_view(file_name, line_num)
4. 编写一个程序,实现“全部替换”功能,程序实现如图:

def file_replace(file_name, rep_word, new_word):
f_read = open(file_name)
content = []
count = 0
for eachline in f_read:
if rep_word in eachline:
count = eachline.count(rep_word) #count感觉应该用这个
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('\n文件 %s 中共有%s个【%s】\n您确定要把所有的【%s】替换为【%s】吗?\n【YES/NO】:' \
% (file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
f_write = open(file_name, 'w')
f_write.writelines(content)
f_write.close()
f_read.close()
file_name = input('请输入文件名:')
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
file_replace(file_name, rep_word, new_word)
边栏推荐
- [Jianzhi offer] interview question 44 A digit in a sequence of numbers
- Operation of simulation test platform for 2022 Shandong safety officer C certificate test
- las 点云创建网格
- Baijia forum Wu Zetian
- Fegin的解析
- 牛客 52次月赛 C 说谎的机器 (区间赋值操作由O(n^2)转为O(n)的复杂度)
- Remote access to raspberry pie via the Internet.
- [876. intermediate node of linked list]
- Use Charles to capture packets
- 90- review of several recently optimized Oracle databases
猜你喜欢

2022 question bank and simulated examination for work license of main principals of hazardous chemical business units

【160. 相交链表】

【206. 反转链表】

NFT,只可远观不可亵玩焉

HarmonyOS应用开发培训第二次
![[142. circular linked list II]](/img/c1/f3a0b863286e9eeda0ae4021420912.png)
[142. circular linked list II]

How swiftui simulates the animation effect of view illumination increase
![[redis]redis persistence](/img/83/9af9272bd485028062067ee2d7a158.png)
[redis]redis persistence
![[redis]集群与常见错误](/img/a5/94906b62b1ec0d549f9b72ff3db7f2.png)
[redis]集群与常见错误
![[redis] profile](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis] profile
随机推荐
53 page intelligent campus intelligent system design scheme (download attached)
[redis]redis6 master-slave replication
Fluent system architecture
94-SQL优化案例一则(用到的写法经常是被嫌弃的)
[513. find the value in the lower left corner of the tree]
数据库总结:mysql在开发过程中常见的问题及优化
HarmonyOS应用开发培训第二次
92 match for several_ Recognize SQL write example
程序员必看的学习网站
第020讲:函数:内嵌函数和闭包 | 课后测试题及答案
[redis]配置文件
第032讲:异常处理:你不可能总是对的 | 课后测试题及答案
513. find the value in the lower left corner of the tree / Sword finger offer II 091 Paint the house
View Apple product warranty status
Japanese anime writers and some of their works
[redis] cluster and common errors
[160. cross linked list]
79- do not create desc descending index when you see order by XXX desc - there is book donation benefit at the end of the article
【链表中倒数第k个结点】
Pytorch's model saving, loading and continuing training