当前位置:网站首页>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()

原网站

版权声明
本文为[ChaseTimLee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222005194087.html