Python programming - file and exception handling

I. Introduction

Key points: You can use a file to save data permanently; you can use exception handling to make the program you write safe, reliable and robust.

        Data used in a program are temporary and will be lost when the program terminates, unless the data is specifically saved. In order to be able to permanently save the data created in the program, they need to be stored in files on disk or CD-ROM. These files can be transferred and subsequently read by other programs.

2. Text input and output

Key point: In order to read data from or write data to a file, you need to use the open function to create a file object and use the read and write methods of this object to read and write data.

        In a file system, each file is stored in a directory. An absolute file name is composed of the file name and its full path and drive letter. For example: c:\pybook\Scores.txt is the absolute file name of the file Scores.txt on the Windows operating system. Here c:\pybook refers to the directory path of the file. Absolute file names are machine dependent.

        On UNIX platforms, the absolute file name may be /home/liang/pybook/Scores.txt, where /home/liang/pybook is the directory path to the file Scores.txt. Relative file names are relative to the file's current working path. The full path of a relative filename is ignored. For example, Scores.py is a relative file name. If its current working path is c:\pybook, then the absolute file name should be c:\pybook\Scores.py.

        Files can be divided into two categories: text files and binary files. A file that can be processed (read, write and create) using a text editor or Notepad on a Windows system or vi on a UNIX system is called a text file (text file). All other files are called binary files. For example, Python source programs are stored in text files and can be processed by a text editor, but Microsoft Word files are stored in binary files and processed by the Microsoft Word program.

        Although not technically correct, you can think of a text file as a sequence of characters and a binary file as a sequence of bits. Characters in text files are encoded using character encoding tables like ASCII and Unicode. For example: the decimal integer 199 is stored as three characters 1, 9 and 9 in the text file, while the same integer is stored as a byte type C7 in the binary file, because decimal 199 is equal to hexadecimal C7 (199 =12x 16'+7). The advantage of binary files is that they can be processed more efficiently than text files.

Note: Computers do not differentiate between binary files and text files. All files are stored in binary format, so in fact all files are binary files. Text IO (input and output) is built on Binary 10 to provide a certain degree of abstraction for character encoding and decoding.

2.1. Open a file

        How to write (read) data to (from) a file? You need to create a file object related to the physical file. This is called opening a file. The syntax to open a file is as follows.

fileVariable = open(filename, mode)

        The open function returns a file object for filename. The mode parameter is a string that specifies how this file will be used (read-only or write-only), as shown in the table below.

For example, the following statement opens a file named Scores.txt in the current directory for reading.

input = open("Scores.txt", "r")

2.2. Write data

        The open function creates a file object, which is an instance of the _io.TextIOWrapper class. This class contains methods for reading and writing data and closing files, as shown in the figure below.

        After a file is opened for writing data, you can use the write method to write a string to the file. In the program listing below, the program writes three strings to the file Presidents.txt.

def main():

    outfile = open("Presidents.txt", "w")
    # Write data to the file
    outfile.write("Bill Clinton\n")
    outfile.write("George Bush\n")
    outfile.write("Barack Obama")
    outfile.c1ose() # Close the output file
   
 main() # Call the main function

        This program uses w mode to open a file named Presidents.txt for writing data. If the file does not exist, the open function creates a new file. If the file already exists, the contents of the file will be overwritten with new data . Now, data can be written to the file.

        When a file is opened for writing or reading, a special mark called a file pointer is placed inside the file. Read or write operations occur at the pointer location. When a file is opened, the file pointer is placed at the beginning of the file. When a file is read or written, the file pointer will be advanced.

        The program calls the write method on the file object to write three strings. The figure below shows the position of the file pointer after each write operation.


        The program finally closes the file to ensure that the data is written to the file. When this program is executed, three names are written to the file. This file can be viewed using a text editor.

Note: When calling print(str), the function will automatically insert a newline character \n after displaying the string. However, the write function does not automatically insert a newline character. You must explicitly write a newline character to the file.

Warning: If you open an existing file for writing, the original contents of the file will be overwritten with new text or destroyed .

2.3. Test the existence of files

        To prevent data in an existing file from being accidentally erased, you should check whether the file already exists before opening it for writing. The isfile function in the os.path module can be used to determine whether a file exists. For example:

import os.path
    
if os.path.isfile("Presidents.txt") :
    print("Presidents.txt exists")

        Here, isfile("Presidents.txt") returns True if the file Presidents.txt exists in the current directory.

2.4. Reading data

        When a file is opened for reading, you can use the read method to read a specific number of characters or all characters from the file and return them as a string, the readline() method reads the next line, and the readlines ( ) method Read all lines and put into a list of strings.

Suppose the Presidents.txt file contains three lines of names. The program in the program listing below reads data from a file.

def main():

    infile = open("Presidents.txt", "r")
    print("(1) Using read()): ")
    print(infile.read())
    infile.close()  # Close the input file

    # Open file for input
    infile = open("Presidents. txt", "r")
    print("\n(2) Using read(number): ")
    s1 = infile.read(4)
    print(s1)
    s2 = infile.read(10)
    print(repr(s2))
    infile.close()  # Close the input file

    # Open file for input
    infile = open("Presidents.txt", "r")
    print("\n(3) Using readline(): ")
    line1 = infile.readline()
    line2 = infile.readline()
    line3 = infile.readline()
    line4 = infile.readline()
    print(repr(line1))
    print(repr(line2))
    print(repr(line3))
    print(repr(line4))
    infile.close()  # Close the input file

    # Open file for input
    infile = open("Presidents.txt", "r")
    print("\n(4) Using read1 ines(): ")
    print(infile.readlines())
    infile.close()  # Close the input file

main() # Ca11 the main function

The output is:

(1) Using read():
Bill Clinton
George Bush
Barack Obama

(2) Using read(number):
Bill
' Clinton\nG'

(3) Using readline():
'Bill Clinton\n'
'George Bush\n'
'Barack Obama'
''

(4) Using readlines():
['Bill Clinton\n', 'George Bush\n',  ' Barack Obama ']

2.5. Read all data from the file

        Programs often need to read all data from a file. Here are two common ways to accomplish this task:

  • 1) Use the read() method to read all the data from the file and return it as a string.
  • 2) Use the readlines() method to read all the data from the file and return it as a list of strings.

        These two methods are simple and effective for small files, but what if the file is so large that its contents cannot be stored in memory? You can write the following loop to read one line of the file at a time, and continue reading. One line until the end of the file:

line = infile.readline() # Read a line
while line != ''
    # Read next line
    line = infile.readline()

Note: readline() returns ' '         when the program reaches the end of the file .
        Python also allows you to use a for loop to read all lines of a file, as shown below.

for line in infile:
    # Process the line here ...

        This is much simpler than using a while loop.
        The following program listing writes a program that copies data from a source file to a target file and counts the number of lines and characters in the file.

import os.path
import sys


def main():
    f1 = input("Enter a source file: ").strip()
    f2 = input("Enter a target file: ").strip()

    # Check if target file exists
    if os.path.isfile(f2):
        print(f2 + " already exists")
        sys.exit()

    # Open files for input and output
    infile = open(f1, "r")
    outfile = open(f2, "w")

    # Copy from input file to output file
    countLines = countChars = 0
    for line in infile:
        countLines += 1
        countChars += len(line)
        outfile.write(line)
    print(countLines, "lines and", countChars, "chars copied")

    infile.close()  # Close the input file
    outfile.close()  # Close the output file


main()  # Call the main function

2.6. Append data

        You can use a mode to open a file to add data to the end of an already existing file. The following program listing gives an example of appending two lines to a file named Info.txt.

def main():

    # Open file for appending data
    outfile = open("Info.txt", "a")
    outfile.write("\nPython is interpreted\n")
    outfile.close()  # Close the file

main() # Call the main function

        The program uses a mode to open a file named Info.txt to add data to the file through the file object oufile. Assume that an existing file contains text data "Programming is fun". The figure below shows the position of the file pointer when the file is opened and after each write. When a file is opened, the file pointer is placed at the end of the file.

 The program finally closes the file to ensure that the data has been correctly written to the file.

3. File dialog box

Key points: The tkinter.filedialog module contains the askopenfilename and asksaveasfilename functions to display file open and save as dialog boxes.

Tkinter provides the tkinter.filedialog module with the following two functions.

# Display a file dialog box for opening an existing file
filename = askopenfilename()
# Display a file dialog box for specifying a file for saving data
filename = asksaveasfilename()

        Both functions return a filename. If the dialog box is canceled by the user, then this function returns None. Below is an example of using these two functions.

1 from tkinter.filedialog import askopenfilename
2 from tkinter.filedialog import asksaveasfilename
3
4 filenameforReading = askopenfilename()
5 print("You can read from" + filenameforReading)
6
7 filenameforWriting = asksaveasfilename()
8 print("You can write data to " + filenameforWriting)

        When you run this code, the askopenfilename() function displays an open dialog box indicating the file to open. The asksaveasfilename() function displays the Save As dialog box to specify the name and path of the file to be saved.

4. Obtain data from the website

Key Point: Use the urlopen function to open a Uniform Resource Locator (URL) and read data from the website.

        You can use Python to write simple code to read data from a web site. All that is required is to open a URL by using the urlopen function as shown below.

infile = urllib.request.urlopen("http: //www.yahoo.com")

        The urlopen function (defined in the urllib.request module) opens a URL resource like a file.
        Below is an example of reading and displaying website content from a given URL.

import urllib.request
infile = urllib.request.urlopen("http://www.yahoo.com/index.html")
print(infile.read().decode())

        The data read from the URL using infile.read() is raw data in bit form. Call the decode() method to convert the original data into a string.

Note: In order for the urlopen function to recognize a valid URL, the URL address needs to be prefixed with http:// .

5. Exception handling

Key Point: Exception handling enables a program to handle exceptions and then continue its normal execution.

        When running the program from the previous section, what happens if the user enters a file or URL that does not exist? The program will break and throw an error. If you enter a file name that does not exist when running a program in the program listing, the program will report this exception .

        These lengthy error messages are called stack traces or tracebacks . Traceback gives information about the statement that caused the error by tracing back to the function call that led to the statement. Display the line number of the function call in the error message to help track the error.

        Errors that occur during runtime are called exceptions. How to handle an exception so that the program can catch the error and prompt the user to enter a correct file name? This can be achieved using Python's exception handling syntax.

        The exception handling syntax is to wrap code that may generate (throw) exceptions in a try clause, as shown below.

try:
    <body>
except <ExceptionType> :
    <handler>

        Here, <body> contains code that may throw exceptions. When an exception occurs, the remaining code in the <body> is skipped. If the exception matches an exception type, the handling code for that type will be executed. <handler> is the code that handles the exception.

The try/except block works as follows:

  • First, the statements between try and except are executed.
  • If no exception occurs, the except clause is skipped. In this case, execute the break statement to exit the while loop.
  • If an exception occurs while executing the try clause, the remainder of the clause will be skipped. In this case, if the file does not exist, the open function will throw an exception and the break statement will be skipped.
  • When an exception occurs, if the exception type matches the exception name after the except keyword, then the except clause is executed, and then execution of the statement after the try statement continues.
  • If an exception occurs but the exception type does not match the exception name in the except clause, then the exception is passed to the caller of this function; if no handler is found to handle the exception, then this is an unhandled exception and the program terminates. error message.

        A try statement can have multiple except clauses to handle different exceptions. This statement can also choose else or finally statement , the syntax is as follows.
 

try:
    <body>
    except <ExceptionType1>:
        <handler1>
    ...
    except <ExceptionTypeN> :
        <handlerN>
    except :
        <handlerExcept>
    else:
        <process_else>
    finally:
        <process_finally>

        Multiple except statements are similar to elif statements. When an exception occurs, it is sequentially checked to see if it matches the exception in the except clause after the try clause. If a match is found, the handler for the matching exception will be executed and the rest of the except clause will be ignored.

        Note: The <ExceptionType> at the end of the except clause may be ignored. If the exception does not match any exception type before the last except clause, then the <handlerExcept> of the last except clause is executed .

        A try statement can have an optional else clause. If no exception is thrown in the try block, the else block will be executed .

        A try statement can have an optional finally block, which is used to define the final action that will be executed regardless of the situation .

6. Throw exception

Key point: Exceptions are wrapped in objects, and objects are created by classes. A function throws an exception.

        In the previous section you learned how to write code that handles exceptions. So where do exceptions come from? How is an exception generated? The information attached to the exception is wrapped in an object. Exceptions arise from a function. When a function detects an error, it creates an object from the correct exception class using the following syntax and throws the exception to the caller of the function.

raise ExceptionClass ("Something is wrong")

        Here's how exceptions work. Suppose the program detects that an argument passed to a function conflicts with the contract of this function; for example, the argument must be non-negative, but the argument passed is a negative number. This program will create an instance of RuntimeError class and throw it as shown below.

ex = RuntimeError("Wrong argument")
raise ex

        Or, if you prefer, you can combine the previous two statements into one statement.

raise RuntimeError("Wrong argument")

        Now you know how to throw and handle exceptions. So what are the benefits of using exception handling? Using exception handling enables a function to throw an exception to its caller. The caller can handle this exception. Without this capability, the called function must handle the exception itself or terminate the program. Usually the called function doesn't know how to handle an error. This is typical for library functions. Library functions can detect errors, but only the caller knows how to handle an error when it occurs. The most important advantage of exception handling is that it separates error detection (done in the called function) from error handling (done in the calling function).

        Many library functions can throw various exceptions, such as ZeroDivisionError, TypeError and IndexError exceptions. These exceptions can be caught and handled using try-except syntax.

Functions may call other functions in a function call chain. Consider the example of multiple function calls. Suppose the main function calls function function1, function function1 calls function function2, function function2 calls function function3, and function function3 throws an exception, as shown in the figure. Consider the following scenario:

  • If the exception type is Exception3, then this exception will be caught by the except block in function function2 that handles this exception. statement5 will be skipped and statement6 will be executed.
  • If the exception type is Exception2, function function2 is aborted, control is returned to function function1, and this exception will be caught by the except block in function functionl that handles Exception2. statement3 will be skipped and statement4 will be executed.
  • If the exception type is Exceptionl, function functionl is terminated and control returns to the main function. This exception will be caught by the except block in the main function main that handles Exceptionl. statement1 will be skipped and statement2 will be executed.

7. Use objects to handle exceptions

Key point: access an exception object in the except clause.

        As mentioned earlier, an exception is wrapped in an object. In order to throw an exception, you need to first create an exception object and then throw it using the raise keyword. Can this exception object be accessed from the except clause? The answer is yes. The exception object can be assigned to a variable using the following syntax.

try
    <body>
except ExceptionType as ex:
    <handler>

        With this syntax, when an exception is caught by the except clause, the exception object is assigned to a variable named ex. Now, this object can be used in the processor.

8. Define custom exception classes

Key point: You can define a custom exception class by extending the BaseException class or a subclass of the BaseException class.

        So far in this chapter, we have used Python's built-in exception classes like ZeroDivisionError, SyntaxError, RuntimeError, and NameError. Are there other exception types that we can use? The answer is yes, Python has many built-in exceptions. The image below shows some of these anomalies.

        Note: The class names Exception, StandardError and RuntimeError are a bit confusing. All three categories are exceptions, and these errors occur at runtime.

        The BaseException class is the parent class of all exception classes . All Python exception classes inherit directly or indirectly from the BaseException class. As you can see, Python provides many exception classes. You can also define your own exception
classes, all of which inherit from the BaseException class or the subclasses of the BaseException class, for example: RuntimeError.

9. Summary

  • You can use file objects to read (write) data from (to) files. You can open a file and use r mode for reading, w mode for writing, and a mode for appending.
  • You can use the os.path.isfile(f) function to check whether a file exists.
  • There is a file class in Python, which contains methods for reading, writing data and closing files.
  • Data can be read from a file using the read(), readline() and readlines() methods.
  • You can use the write(s) method to write a string to a file.
  • Close the file after processing to ensure the data is saved correctly.
  • Resources can be read from a web page just like data is read from a file.
  • Use exception handling to catch and handle runtime errors. Place the code that throws the exception in the try clause, list the exceptions in the except clause, and handle the exception in the except subdirectory.
  • Python provides built-in exception classes like ZeroDivisionError, SyntaxError and RuntimeError. All Python exception classes inherit directly or indirectly from the BaseException class. You can also define your own exception class. The custom exception class extends from the BaseException class or its subclasses, like RuntimeError.

Guess you like

Origin blog.csdn.net/java_faep/article/details/132332799