当前位置:网站首页>Lesson 029: Documents: a task? After class test questions and answers
Lesson 029: Documents: a task? After class test questions and answers
2022-06-22 21:35:00 【ChaseTimLee】
use one's hands :
0. Write a program , Accept user input and save it as a new file , The program implementation is shown in the figure :

def file_write(file_name):
f = open(file_name, 'w')
print(' Please enter the content 【 Separate input \':w\' Save and exit 】:')
while True:
write_some = input()
if write_some != ':w':
f.write('%s\n' % write_some)
else:
break
f.close()
file_name = input(' Please enter filename :')
file_write(file_name)
1. Write a program , Compare two files entered by the user , If different , Show all the different line numbers and the position of the first different character , The program implementation is shown in the figure :

def file_compare(file1, file2):
f1 = open(file1)
f2 = open(file2)
count = 0 # The statistical number of rows
differ = [] # Count different numbers
for line1 in f1:
line2 = f2.readline()
count += 1
if line1 != line2:
differ.append(count)
f1.close()
f2.close()
return differ
file1 = input(' Please enter the first file name to compare :')
file2 = input(' Please enter another file name to compare :')
differ = file_compare(file1, file2)
if len(differ) == 0:
print(' The two files are exactly the same !')
else:
print(' The two files share 【%d】 Different :' % len(differ))
for each in differ:
print(' The first %d It's different ' % each)
2. Write a program , When the user enters the file name and the number of lines (N) after , Put the front of the file N The line content is printed to the screen , The program implementation is shown in the figure :

def file_view(file_name, line_num):
print('\n file %s Before %s Is as follows :\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' Please enter the file to open (C:\\test.txt):')
line_num = input(' Please enter the first few lines of the file to be displayed :')
file_view(file_name, line_num)
3. Uh , It has to be said that our users are becoming more and more tricky . It is required to expand on the previous question , The user can input the number of lines to be displayed at will .( Such as the input 13:21 Print page 13 Go to the first place 21 That's ok , Input :21 Before printing 21 That's ok , Input 21: Then print from 21 Everything from the beginning of the line to the end of the file )

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 = ' Full text '
elif begin == '1':
prompt = ' From the beginning to %s' % end
elif end == '-1':
prompt = ' from %s To end ' % begin
else:
prompt = ' From %s Go to the first place %s That's ok ' % (begin, end)
print('\n file %s%s Is as follows :\n' % (file_name, prompt))
begin = int(begin) - 1
end = int(end)
lines = end - begin
f = open(file_name)
for i in range(begin): # To consume begin Previous content
f.readline()
if lines < 0:
print(f.read())
else:
for j in range(lines):
print(f.readline(), end='')
f.close()
file_name = input(r' Please enter the file to open (C:\\test.txt):')
line_num = input(' Please enter the number of rows to display 【 The format is as follows 13:21 or :21 or 21: or : 】:')
file_view(file_name, line_num)
4. Write a program , Realization “ All replacement ” function , The program implementation is shown in the figure :

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 I think I should use this
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('\n file %s The Communist Party of China has %s individual 【%s】\n Are you sure you want to put all the 【%s】 Replace with 【%s】 Do you ?\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(' Please enter filename :')
rep_word = input(' Please enter the word or character to be replaced :')
new_word = input(' Please enter a new word or character :')
file_replace(file_name, rep_word, new_word)
边栏推荐
- 第031讲:永久存储:腌制一缸美味的泡菜 | 课后测试题及答案
- Apple Objective-C source code
- las 点云创建网格
- One line of code binds swiftui view animation for a specific state
- MySQL adds (appends) prefix and suffix to a column field
- [redis]配置文件
- 数据库总结:mysql在开发过程中常见的问题及优化
- 2022 a special equipment related management (elevator) examination questions and simulation examination
- 杰理之开启四声道通话近端卡顿问题【篇】
- 第033讲:异常处理:你不可能总是对的2 | 课后测试题及答案
猜你喜欢
![[513. find the value in the lower left corner of the tree]](/img/6d/b2ec8e3072a65c20c586941e6b2a85.png)
[513. find the value in the lower left corner of the tree]

第026讲:字典:当索引不好用时2 | 课后测试题及答案
![Jerry's dynamic switching EQ document [chapter]](/img/2d/9a0245b87fb05ea61dbfc7922ea766.png)
Jerry's dynamic switching EQ document [chapter]

Fluent system architecture

第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案

Arcgis中las点云数据抽稀

2022年山东省安全员C证考试试题模拟考试平台操作

引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能

Cannot re-register id: PommeFFACompetition-v0问题解决
![Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver](/img/29/8dd188cc4e909562862b5f2c57c898.png)
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
随机推荐
杰理之开启四声道通话近端卡顿问题【篇】
RealNetworks vs. 微软:早期流媒体行业之争
70 root cause analysis Oracle database sudden performance problems, who will take the blame
localStorage、sessionStorage 和 cookie 的区别大总结
The second harmonyos application development training
Jerry's music mode obtains the directory of playing files [chapter]
杰理之使用 DP 和 DM 做 IO 按键检测注意点【篇】
When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]
71- analysis of an Oracle DBA interview with Alibaba in 2010
85- have you learned any of these SQL tuning tips?
Fegin的解析
Apple GCD source code
Adblock屏蔽百度热搜
[redis]redis persistence
第027讲:集合:在我的世界里,你就是唯一 | 课后测试题及答案
84- I am on Internet & lt; 52 SQL statement performance optimization strategies & gt; Some views of
牛客 52次月赛 B牛牛的身高 (思维题 模拟题)
Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
[160. cross linked list]
List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]