Python Notes for Files & Exceptions

Posted by ebgames56 on Wed, 08 May 2019 22:54:03 +0200

Python Notes for Files & Exceptions

Article directory

1. Read data from files

1.1 Read the entire file

Open the file with an open() statement and read the entire file with read().

#pi10000.txt is a file containing 10,000 bits after the PI decimal point
pi='/home/choupin/desktop/pi10000.txt'
with open(pi) as file_object:
    contents=file_object.read()
    print(contents.rstrip())

It is more convenient to store file paths and names in variables.

Keyword with: Close the file after you no longer need to access it; the file object returned by open() is available only in the with code block.

read() returns a string when it reaches the end of the file. To delete the extra empty lines, rstrip() can be used in the print statement.

1.2 File Path

When the filename is passed to the function open(), Python looks for the file in the directory where the currently executed file is located.

Relative path: The path from the current file to the target file.

Absolute path: The actual path of the target file.

1.3 Line-by-line reading

You can use for loops on file objects.

pi='/home/choupin/desktop/pi10000.txt'
with open(pi) as file_object:
    for line in file_object:
        print('line')

Because the text file has a newline at the end of each line, and the print statement also adds a newline, the orphan will produce redundant blank lines.

Similarly, rstrip() can be used in print statements to eliminate these redundant blank lines.

1.4 Create a list of all lines of a file

In order to access the contents of files outside the with code block, the contents of files can be stored in a list within the code block and used outside the with code block. as

#pishorter is a file containing 315 digits after the PI decimal point
pi='/home/choupin/desktop/pishorter.txt'
with open(pi) as file_object:
    lines=file_object.readlines()
for line in lines:
    print(line)

Here readlines() read every line in the file, so lines are a list of elements based on each line in the file.

Note: readline() reads only one line. If used here, lines are a list of elements with each character in a line.

1.5 Contents of User Files

Once the file is read into memory, it can use the data in any way.

For example, here the data in file Pi is stored in a string without spaces.

In order to eliminate the space in the file, strip() instead of rstrip() is used when storing strings.

#pishorter is a file containing 315 digits after the PI decimal point
with open(pi) as file_object:
    lines=file_object.readlines()
pi_string=''
for line in lines:
    pi_string+=line.strip()
print(pi_string)
print(len(pi_string))

Note: When reading text files. By default, Python interprets all text as strings. If you want to use the read number as a numerical value, you should use int() or float() conversion.

1.6 Processing large files

There is no difference in the method of operation.

Python has no limitations on the amount of data that can be processed. That is, as long as the system has enough memory, it can process as much data as it wants.

2 Write to file

2.1 Write empty files

The second argument of the open() function specifies the mode of opening the file: read mode ('r'), write mode ('w'), add mode ('a'), read and write mode ('r+).

When the written file does not exist, the open() function automatically creates the file.

filename='/home/choupin/desktop/ceshi.txt'

with open(filename,'w') as file_obj:
    file_obj.write(('I love Lesy!'))

tip: Writing will not be saved to the file until after the end of the with statement block. If it is read in the same statement block, it will not be able to read the written content.

** Note: ** In write mode `w', if the specified file already exists, Python will empty the file before returning the file object.

Note: Python can only write strings to a file. If you need to store numerical data in a text file, you must first convert it into a string using the str() function.

2.2 Write multiple lines

write() does not add a newline at the end of the text being written, just add'\ n'to the content.

Like the output of a display terminal, the output format can also be set with spaces and tabs and blank lines.

2.3 Attach to File

You can add content to the end of the file by using the ** add mode `a'**.

If the specified file does not exist, an empty file is created.

3 exceptions

Python uses special objects that are exceptions to manage errors that occur during program execution.

When an error occurs, Python creates an exception. If there is code to handle the exception in the program, it continues to execute. Otherwise, the program stops and displays a traceback containing a report on the exception.

Exceptions are handled using try-except code blocks. This code block enables Python to perform specified operations after an exception is generated, keeps the program running, and displays user-friendly error messages instead of traceback.

3.1 Handling Zero Division Error Abnormality

Taking 0 as a divisor generates this exception. Such as:

print(5/0)

Will see

Traceback (most recent call last):
  File "/home/choupin/File/PythonPractice/filepractice/ZeroError.py", line 1, in <module>
    print(5/0)
ZeroDivisionError: division by zero

3.2 Use try-except code block

Where errors may occur, you can write a try-except code block to handle the exception that may be thrown.

If you can write try-except code blocks to process ZeroDivision Error:

try:
    print(5/0)
except ZeroDivisionError:
    print('You can\'t divide by zero!')

That is, if the code in the try block runs well. If an error occurs, the except code block is run to find the same error, and the corresponding operation is given as a prompt. The program will then run.

3.3 else code block

By putting blocks of code that may cause errors in try-except blocks, the program's ability to resist errors can be improved.

The except code block contains only the code that runs after the corresponding error occurs, while the else code block contains the code that runs after the error does not occur.

3.4 Handling FileNotError Exceptions

This exception is thrown when open() reads a non-existent file. The exception can be handled using the try-expect code block as follows:

filename='alien.txt'
try:
    with open(filename) as f_obj:
        contents=f_obj.read()
 except FileNotFoundError:
    msg='Sorry,the file '+filename+' does not exist.'
    print(msg)

3.5 Analytical Text

split(): Splits a string into word lists with spaces in the string as separators.

Using this method, we can get a list of words in a text, and analyze the text, such as calculating the word book of the text, the frequency of words appearing, etc.

filename='/home/choupin/desktop/ceshi.txt'
#Ordinary Edition
# with open(filename) as file_obj:
#     contents=file_obj.read()
#     words=contents.split()
# print(len(words))

#Handling exception Edition
try:
    with open(filename) as file_obj:
        contents=file_obj.read()
except FileNotFoundError:
    msg='Sorry,the file '+filename+' does not exist!'
    print(msg)
else:
    words=contents.split()
    num_words=len(words)
    print('The file '+filename+' has about '+str(num_words)+' words.')

3.6 Use multiple files

The program that uses files is converted into a function, which can be called separately when multiple files are needed.

For example, the small program for analyzing the number of words in the text in the preceding section can be transformed into a function:

def count_words(filename):
    try:
        with open(filename) as file_obj:
            contents=file_obj.read()
    except FileNotFoundError:
        msg='Sorry,the file '+filename+' does not exist!'
        print(msg)
    else:
        words=contents.split()
        num_words=len(words)
        print('The file '+filename+' has about '+str(num_words)+' words.')

3.7 No abnormal reminders

The pass statement allows the block to do nothing and continue to execute other parts.

By using pass statements in except code blocks, the program can handle exceptions without prompting.

def count_word(filename):
    try:
        --sinp--
    except FileNotFoundError:
        pass
    else:
        --sinp--

When writing a program, you should decide where you need to report errors to the user and where you don't need them, because displaying unwanted error information to the user will reduce the ease of use of the program.

4 Storage data

Using module json, a simple Python data structure can be dumped into a file and loaded when the program runs again.

You can also use json to share data between Python programs.

JSON data format is not Python-specific, so it can be shared with other programming languages.

Note: JSON format was originally developed for JavaScript, then became a common format and was adopted by many languages.

4.1 Use json.dump() and json.load()

** json.dump()** is used to store data, which accepts two arguments: the data to be stored and the file object to be used to store data.

Let's demonstrate using json.dump() to store a set of numbers:

import json

number=[1,2,3,4,5]
#Note that the suffix of the file name should be json, and if it is in other formats, create other format files
filename='home/choupin/desktop/digitaldata.json'

with open(filename,'w') as file_obj:
    json.dump(number,file_obj)

** json.load()** is used to read data into memory and accept file objects as arguments.

Take the example above:

with open(filename) as file_obj:
    numbers=json.load(file_obj)
print(numbers)

4.2 Save and read user-generated data

For user generated data, if it is not stored in some way, the user's information will be lost when the program stops running. Therefore, it is necessary to design programs to save and read the necessary user data.

Here is a demonstration of a program that saves the user name at the first run time and then reads the user name to greet at the later run time:

import json

filename='/home/choupin/desktop/username.json'

try:
    with open(filename) as file_obj:
        username=json.load(file_obj)
except FileNotFoundError:
    with open(filename,'w') as file_obj:
        username=input('What is your name?')
        json.dump(username,file_obj)
        print('We\'ll remember you when you come back,'+username)
else:
    print('Welcome back, '+username+'!')

4.3 reconstruction

Code can run correctly, and to further improve the code, the code is divided into a series of functions to complete specific work, the process is refactoring.

The purpose is to make the code clearer, easier to understand, and easier to expand.

Here, the program in the previous section can be reconstructed into three functions: one is responsible for storing the user's name at the first run time, the other is responsible for reading the user's name at the subsequent run time, and the other is responsible for calling the two functions to greet the user.

import json

def get_stored_username():
    '''Read stored user names'''
    filename = '/home/choupin/desktop/username.json'
    try:
        with open(filename) as file_obj:
            username=json.load(file_obj)
    except FileNotFoundError:
        #Return None if not stored
        return None
    else:
        return username

def get_new_username():
    '''Prompt the user to enter the username and store it'''
    filename = '/home/choupin/desktop/username.json'
    username=input('What is your name?')
    with open(filename,'w') as file_obj:
        json.dump(username,file_obj)

def greet_user():
    '''Greet the user and call the function to store his username if it runs for the first time'''
    username=get_stored_username()
    if username:
        print('Welcome back, '+username+'!')
    else:
        username=get_new_username()
        print('We\'ll remember you when you come back,'+username)

greet_user()

Topics: JSON Python Programming Javascript