"Python Language Programming" Wang Kai Wang Zhi Machinery Industry Press Chapter 7 I/O Programming and Abnormal After-school Exercises Answers

7.7 Exercises after class
(1) Use the os module to check the name of the current system, you should use os.name.

(2) To use the os module to obtain the separator of the path of the current system platform, os.sep should be used.

(3) To use the os module to obtain the current working directory, the os.getcwd method should be used.

(4) Use the os module to create a directory, you can use the os.mkdir method or the os.makedirs method.

(5) The method to judge whether the specified path target is a file is os.path.isfile.

(6) The method for judging whether the specified path exists in the os module is os.path.exists.

(7) According to the closed attribute of the file object, it can be judged whether the file is closed.

(8) Use the seek method of the file object to move the file pointer, so as to realize random reading and writing of files.

(9) Use the writerow method or writerows method of the writer object to write data to the CSV file.

(10) The method used in the os module to sequentially create all directories that do not exist in the path is (A).

         A. makedirs        B. makedir          C. mkdirs           D. mkdir

(11) Among the following statements, the wrong one is (B).

         A. If the directory to be created already exists, the os.mkdir function will report an error

         B. If the directory to be created already exists, the os.makedirs function will not report an error

         C. If the directory to be deleted does not exist, the os.rmdir function will report an error

         D. If the directory to be deleted already exists but the directory is not empty, the os.rmdir function will report an error

(12) The method used in the os module to delete the last multi-level directory of the specified path is (A).

         A. removedirs          B. removedir          C. rmdirs           D. rmdir

(13) The default opening mode of the open function is (C).

         A. w              B. w+           C. r             D. r+

(14) Among the following file opening methods, the one that cannot write to the opened file is (C).

         A. w              B. wt            C. r             D. a

(15) To read all the data line by line from the file, you should use (D) method.

         A. read          B. readall        C. readline           D. readlines

(16) Regardless of whether an exception occurs during the execution of the try clause, the clause that will be executed is (B).

         A. else B. finally C. except D. does not exist

(17) Write down the running result of the following program.

for i in range(3):
    try:
        num=(i+1)*5
        assert num%2!=0
        print(num)
    except AssertionError:
        print('Assertion failed! num=%d'%num)
 
 
#output result
5
Assertion failed! num=10
15
(18) Write the running result of the following program.

import os
for n in os.path.split('D:\\mydir\\subdir1\\test.txt'):
    print(n)
 
 
#output result
D:\mydir\subdir1
test.txt
(19) The following program is in Create a file named test.txt in the mydir directory of disk D and write the string "Nankai University" into the file, please fill in the program completely.

with open('D:\\mydir\\test.txt','w+') as f:
    f.write('Nankai University')
(20) The following program creates a file named score under the mydir directory of the D disk. csv file, and write the grades of 3 courses of 2 students into the file, please fill in the program completely.

import csv #Import csv module
data2D=[[90,98,87], #The grades of the 3 courses of the first student
[70,89,92]] #The grades of the 3 courses of the second student
with open('D :\\mydir\\score.csv','w',newline='') as f: #open file
    csvwriter=csv.writer(f)
    csvwriter.writer(['Chinese','mathematics','English' ]) #Write the column headings into the CSV file first
    csvwriter.writerows(data2D) #Write the data in the two-dimensional list into the CSV file
(21) The UserError below is a custom exception class, please fill in the program completely.

class UserError(BaseException):
    def __init__(self,msg): #Define the constructor
        self.msg=msg
    def __str__(self): #Automatically call
        return self.msg when converting the UserError class object to a string

Guess you like

Origin blog.csdn.net/weixin_49647262/article/details/122028343