当前位置:网站首页>Lesson 028: Documents: because I know you, I will never forget the after-school test questions and answers [no title]
Lesson 028: Documents: because I know you, I will never forget the after-school test questions and answers [no title]
2022-06-22 21:36:00 【ChaseTimLee】
Test questions :
0. There is only one way to not open a file , What kind is it, please , Why? ?
>>> f = open('E:/test.txt', 'w') # A
>>> f = open('E:\test.txt', 'w') # B
>>> f = open('E://test.txt', 'w') # C
>>> f = open('E:\\test.txt', 'w') # D
answer :B Can't open file .
Windows Slashes are acceptable in pathnames (/) Backslashes are acceptable (\), However, if you use a backslash as a separator for path names , Be careful to use double backslashes (\) Transference , otherwise Python Will escape the backslash , for example (\n) As a newline character ,(\t) As a tab, etc .
1. Open a file we use open() function , By setting the open mode of the file , Decide what properties the open file has , What is the default open mode ?
answer :open() The default open mode of the function is ’rt’, You can read 、 Text mode is turned on .
2. Excuse me, >>> open(‘E:\Test.bin’, ‘xb’) In what mode is the file opened ?
answer : With “ Writable and binary mode ” Open file “E:\Test.bin”.
What we should pay attention to here is ’x’ and ’w’ All of them are based on “ Writable ” Open the file in , But with ’x’ When mode is on , If the same file name already exists in the path , It throws an exception , and ’w’ Mode will directly overwrite the file with the same name .
therefore ,'w’ Opening a file in mode can be dangerous , It is easy to cause the previous content to be lost , Therefore use ’w’ It is very important to check whether the file name already exists before opening the file in the mode ! The next lesson little turtle will teach you how to open a file safely _
3. Even though Python There are so-called “ Garbage collection mechanism ”, But for open files , We still need to use... When we don't need it f.close() Put the file object “ close ”, Why is that ?
answer :Python Have a garbage collection mechanism , Automatically closes the file when the reference count of the file object drops to zero , So in Python In programming , If you forget to close the file, it will not cause a memory leak .
But it does not mean that you can not close the file , If you write to a file , Then you should close the file after writing . because Python It may cache the data you write , If the power is cut off in the middle , The cached data will not be written to the file at all . therefore , For safety's sake , Get into the elegant habit of closing files immediately after using them .
4. How to set a file object (f) The data in is stored in the list ?
answer :list(f), Is it very convenient !
5. How to iteratively print out file objects (f) Every row of data in ?
answer : Use it directly for Statement to iterate out the file object :
for each_line in f:
print(each_line)
6. Built in methods for file objects f.read([size=-1]) The function is to read the contents of file objects ,size Parameters are optional , If it is set size=10, for example f.read(10), What will be returned ?
answer : Will return starting from the file pointer ( Note that this is not the file header ) Continuity of 10 Characters .
7. How to get file objects (f) The location of the current file pointer ?
answer :f.tell() I will tell you. _
8. Or the demo file in the video (record.txt), Why f.seek(45, 0) No mistakes , but f.seek(46) I made a mistake ?
>>> f.seek(46)
46
>>> f.readline()
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
f.readline()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xe3 in position 4: illegal multibyte sequence
answer : Because use f.seek() The located file pointer is calculated in bytes , Presentation files (record.txt) In order to GBK Coded , According to the rules , A Chinese character needs two bytes ,f.seek(45) The position of is in the character “ Small ” The beginning of , So it can print normally , and f.seek(46) The position of is just in the character “ Small ” In the middle of , So according to GBK The encoded form cannot decode it !
use one's hands :
0. Try to save the file ( OpenMe.mp3 (700 Bytes, amount of downloads : 19580) ) Print it to the screen
answer : You can open it directly in the form of an open text file , As for why ? I will tell you when I open it @[email protected]
f = open('OpenMe.mp3')
for each_line in f:
print(each_line, end='')
f.close()
1. Write code , Add the file in the previous question (OpenMe.mp3) Save as new file (OpenMe.txt)
f1 = open('OpenMe.mp3')
f2 = open('OpenMe.txt', 'x') # Use ”x” Open more safely
f2.write(f1.read())
f2.close()
f1.close()
边栏推荐
- Adblock屏蔽百度热搜
- 微信小程序批量提交审核
- [513. find the value in the lower left corner of the tree]
- redis学习笔记
- [redis]Redis6的主从复制
- NFT,只可远观不可亵玩焉
- Objective-C byte size occupied by different data types
- Final review of scientific and technological literature of Shandong University (Personal Crash Course)
- One line of code binds swiftui view animation for a specific state
- 300. 最长递增子序列 ●●
猜你喜欢

软考必备资料大放送,全科目软考资料都给你备好了!
![[cm11 linked list splitting]](/img/66/6ac3f78db20ec7f177b88c88028dde.png)
[cm11 linked list splitting]

第026讲:字典:当索引不好用时2 | 课后测试题及答案

2022 chemical automation control instrument examination exercises and online simulation examination
![DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]](/img/8a/ce164a5538bd8edf10eba5e4e8abe6.png)
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]

300. 最长递增子序列 ●●

长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!

2022年起重机械指挥考试模拟100题及模拟考试

300. longest increasing subsequence ●●

【ICML2022】利用虚拟节点促进图结构学习
随机推荐
One line of code binds swiftui view animation for a specific state
2022危险化学品经营单位主要负责人上岗证题库及模拟考试
[513. find the value in the lower left corner of the tree]
2022年A特种设备相关管理(电梯)考题及模拟考试
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案
Jericho uses DP and DM for IO key detection [chapter]
72 results and development suggestions of the latest on-site production system optimization
300. 最长递增子序列 ●●
86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
[records of different objects required by QIPA]
杰理之使用 DP 和 DM 做 IO 按键检测注意点【篇】
92 match for several_ Recognize SQL write example
513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
[redis]redis persistence
[redis]redis的持久化操作
Apple Objective-C source code
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
[redis] three new data types
513. find the value in the lower left corner of the tree / Sword finger offer II 091 Paint the house