Python Files Exceptions and Modules
December 23, 2024 · 12 min read · Page View:
If you have any questions, feel free to comment below.
This article will review the basic knowledge of Python, including files, exceptions, and modules.
CH7 Files, Exceptions, and Modules #
File Read and Write #
File Open #
- The general format for opening a file, recommended to use with, because if not using with, then you need to consider the close operation
# The advantage of using with block: automatically close the file after execution
with open("file path", "open mode", encoding = "character encoding of the file") as f:
"file read and write operation"
with open("E:\ipython\test.txt", "r", encoding = "gbk") as f:
text = f.read()
print(text)
Open Mode #
r
Read mode,if the file does not exist, an error will be reportedw
Overwrite write mode,if the file does not exist, it will be created; if the file exists, it will completely overwrite the original filex
Create write mode,if the file does not exist, it will be created; if the file exists, an error will be reporteda
Append write mode,if the file does not exist, it will be created; if the file exists, it will be added to the original fileb
Binary file mode,cannot be used alone, it needs to be used together withrb
,wb
,ab
, this mode does not need to specify encodingt
Text file mode,default value, it needs to be used together withrt
,wt
,at
, generally omitted, abbreviated asr
,w
,a
+
,withr
,w
,x
,a
, it adds read and write functions to the original functionsThe default open mode is read mode
Character Encoding #
- Universal code utf-8:includes all characters needed by all countries
- Chinese encoding gbk:used to solve the problem of Chinese encoding, in windows, if omitted, it defaults to gbk (the encoding of the region)。
- For clarity, except for processing binary files, it is recommended not to omit encoding
File Read #
- Read the entire content——
f.read()
with open("never_gonna_give_you_up_utf.txt", "r", encoding="utf-8") as f:
text = f.read()
print(text)
# with open("never_gonna_give_you_up_utf.txt", "r") as f:
# UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 50: illegal multibyte sequence
- Read line by line——
f.readline()
with open("never_gonna_give_you_up_utf.txt", "r", encoding="utf-8") as f:
while True:
text = f.readline() # Read each line
if not text:
# print(text is "")
break
else:
# print(text == "\n") # The newline character is not considered empty
print(text, end="") # Keep the original newline, so that the print() newline does not take effect, because print itself also has a newline effect
- Read all lines, forming a list with each line as an element
f.readlines()
with open("never_gonna_give_you_up_utf.txt", "r", encoding="utf-8") as f:
text = f.readlines() # Note that each line has a newline character at the end
print(text)
# ['never gonna give you up\n', 'never gonna let you down\n', 'never gonna run around and desert you\n', 'never gonna make you cry\n', 'never gonna say goodbye\n', 'never gonna tell a lie and hurt you\n']
for text in f.readlines():
print(text) # If you don't want to change lines, use print(text, end="")
- Text file read summary
When the file is large, read()
and readlines()
occupy too much memory, not recommended. readline
is not convenient to use
with open("never_gonna_give_you_up_gbk.txt", "r", encoding="gbk") as f:
for text in f: # f itself is an iterable object, and each iteration reads one line of content
print(text)
- Binary file
Image: binary file
with open("test.jpg", "rb") as f:
print(len(f.readlines())) # 69
File Write #
- Write a string or byte stream (binary) to a file——
f.write()
with open("恋曲1980.txt", "w", encoding="utf-8") as f:
f.write("你曾经对我说\n") # If the file does not exist, it will immediately create one
f.write("你永远爱着我\n") # If you need to change lines, add a newline character \n at the end, and the file will be written on the second line
- Write a list of strings to a file——
f.writelines()
ls = ["春天刮着风\n", "秋天下着雨\n", "春风秋雨多少海誓山盟随风远去\n"]
with open("恋曲1980.txt", "w", encoding="utf-8") as f:
f.writelines(ls)
Read and Write #
r+
- If the file name does not exist, an error will be reported
- The pointer is at the beginning, and the pointer must be moved to the end to start writing, otherwise it will overwrite the previous content
with open("浪淘沙_北戴河.txt", "r+", encoding="gbk") as f:
# for line in f:
# print(line) # After reading all, the pointer reaches the end
f.seek(0,2) # Or you can move the pointer to the end f.seek(offset in bytes, position (0: start; 1: current position; 2: end))
text = ["萧瑟秋风今又是,\n", "换了人间。\n"]
f.writelines(text)
w+
- If the file does not exist, it will be created, and if the file exists, it will be immediately cleared.
with open("浪淘沙_北戴河.txt", "w+", encoding="gbk") as f:
text = ["萧瑟秋风今又是,\n", "换了人间。\n"] # Clear the original content
f.writelines(text) # Write new content, pointer at the end
f.seek(0,0) # Move the pointer to the start
print(f.read()) # Read content
a+
: If the file does not exist, it will be created, and if the file exists, the pointer is at the end, and new content will be added, without clearing the original content.
Data Storage and Reading #
Common data formats can be loaded and stored in different languages
- csv format
A character sequence separated by commas that can be opened by excel
- Read
with open("score.csv", "r", encoding="gbk") as f:
ls = []
for line in f: # Read line by line
ls.append(line.strip("\n").split(",")) # Remove the newline character and split by ","
for res in ls:
print(res)
- Write
ls = [['id', 'math', 'chinese'], ['1', '100', '98'], ['2', '96', '99'], ['3', '97', '95']]
with open("score.csv", "w", encoding="gbk") as f: # encoding="utf-8"
for row in ls: # Write line by line
f.write(",".join(row)+"\n") # Combine with commas, add a newline character at the end
The above operations can also be completed using the csv module
2、json format
Often used to store dictionaries
- Write -
dump()
import json
scores = {"Petter":{"math":96 , "physics": 98},
"Paul":{"math":92 , "physics": 99},
"Mary":{"math":98 , "physics": 97}}
with open("score.json", "w", encoding="utf-8") as f: # Write the entire object
# indent pretty print, ensure_ascii=False display Chinese
json.dump(scores, f, indent=4, ensure_ascii=False)
- Read -
load()
with open("score.json", "r", encoding="utf-8") as f:
scores = json.load(f) # Load the entire object
for k,v in scores.items():
print(k,v)
# Petter {'math': 96, 'physics': 98}
# Paul {'math': 92, 'physics': 99}
# Mary {'math': 98, 'physics': 97}
Exception Handling #
When an exception occurs, if no pre-set processing method is provided, the program will interrupt
Common Exception Generation #
- Division by zero -
ZeroDivisionError
1/0
# ZeroDivisionError: division by zero
- File not found -
FileNotFoundError
with open("nobody.csv") as f:
pass
# FileNotFoundError: [Errno 2] No such file or directory: 'nobody.csv'
- Value error -
ValueError
Passing a value that is not expected by the caller, even though the value is of the correct type
s = "1.3"
n = int(s)
# ValueError: invalid literal for int() with base 10: '1.3'
- Index error -
IndexError
Index out of sequence boundary
ls = [1, 2, 3]
ls[5]
# IndexError: list index out of range
- Type error -
TypeError
Passing an object type that does not match the requirement
1 + "3"
# TypeError: unsupported operand type(s) for +: 'int' and 'str'
- Other common exception types
NameError - Using an undefined variable
KeyError - Attempting to access a key that does not exist in a dictionary
print(a)
# NameError: name 'a' is not defined
d = {}
d["1"]
# KeyError: '1'
Exception Handling #
Improve the stability and reliability of the program
try except
If the try block code runs smoothly, the except block is not triggered
If an error occurs in the try block, the except block is triggered, and the code in the except block is executed
Single branch
x = 10
y = 0
try:
z = x/y
except ZeroDivisionError: # Generally, it is predicted what error will occur
print("0 cannot be divided!")
# 0 cannot be divided!
- Multiple branches
ls = []
d = {"name": "timerring"}
try:
y = m
# ls[3]
# d["age"]
except NameError:
print("Variable name does not exist")
except IndexError:
print("Index out of bounds")
except KeyError:
print("Key does not exist")
# Variable name does not exist
- Universal exception
Exception
(the ancestor of all errors)
ls = []
d = {"name": "timerring"}
try:
# y = m
ls[3]
# d["age"]
except Exception:
print("Error")
# Error
- Capture the value of the exception as
ls = []
d = {"name": "timerring"}
# y = x
try:
y = m
# ls[3]
# d["age"]
except Exception as e: # Although it cannot obtain the specific type of error, it can obtain the reason for the error
print(e)
# name 'm' is not defined
try except else
- If the try module is executed, the else module is also executed, and the else module can be seen as an additional reward for the successful try.
try:
with open("files.txt") as f:
text = f.read()
except FileNotFoundError:
print("File not found")
else:
for s in ["\n", ",", "。", "?"]: # Remove newline and punctuation characters
text = text.replace(s, "")
print("files consists of {} characters.".format(len(text)))
# files consists of 65 characters.
try except finally
- Whether the try module is executed, finally is executed
ls = []
d = {"name": "timerring"}
# y = x
try:
y = m
# ls[3]
# d["age"]
except Exception as e: # Although it cannot obtain the specific type of error, it can obtain the value of the error
print(e)
finally:
print("Whether an exception is triggered, it will be executed")
# name 'm' is not defined
# Whether an exception is triggered, it will be executed
Module Introduction #
Already encapsulated, no need to “invent the wheel” yourself, declare import, and use it directly.
Broad module classification #
Python built-in #
Time library time
Random library random
Container data type collection
Iterator function itertools
Third-party library #
Data analysis numpy
, pandas
Data visualization matplotlib
Machine learning scikit-learn
Deep learning Tensorflow
Custom file #
- Separate py file
- Package contains multiple py files, needs to add an
__init__.py
file.
Module import #
- Import the entire module——
import module name
- Call method: module name.function name or class name
import time
start = time.time() # Call time module's time()
time.sleep(3) # Call time module's sleep()
end = time.time()
print("{:.2f}s".format(end-start))
- Import classes or functions from a module——
from module import class name or function name
- Call method: function name or class name
from itertools import product # This case can directly use the function name for use, without adding the previous module name
ls = list(product("AB", "123"))
# Import multiple
from function import fun1, fun2
fun1.f1()
fun2.f2()
- Import all classes and functions from a module——
from module import *
(not recommended)
- Call method: function name or class name
from random import * # Wildcard import all files, so you can use without adding the module name prefix
print(randint(1,100))
print(random())
Module search path #
Module search order:
- Priority memory loaded module
- Built-in module
- When Python starts, the interpreter will load some
modules
intosys.modules
sys.modules
variable contains a dictionary of modules loaded into the interpreter (fully and successfully imported), with the module name as the key and its location as the value
- When Python starts, the interpreter will load some
import sys
print(len(sys.modules)) # 738
print("math" in sys.modules) # True
print("numpy" in sys.modules) # False
for k,v in list(sys.modules.items())[:20]:
print(k, ":", v)
# sys : <module 'sys' (built-in)>
# builtins : <module 'builtins' (built-in)>
# _frozen_importlib : <module 'importlib._bootstrap' (frozen)>
# _imp : <module '_imp' (built-in)>
# _thread : <module '_thread' (built-in)>
# _warnings : <module '_warnings' (built-in)>
# _weakref : <module '_weakref' (built-in)>
# zipimport : <module 'zipimport' (built-in)>
# _frozen_importlib_external : <module 'importlib._bootstrap_external' (frozen)>
# _io : <module 'io' (built-in)>
# marshal : <module 'marshal' (built-in)>
# nt : <module 'nt' (built-in)>
# winreg : <module 'winreg' (built-in)>
# encodings : <module 'encodings' from 'C:\\Users\\ibm\\Anaconda3\\lib\\encodings\\__init__.py'>
# codecs : <module 'codecs' from 'C:\\Users\\ibm\\Anaconda3\\lib\\codecs.py'>
# _codecs : <module '_codecs' (built-in)>
# encodings.aliases : <module 'encodings.aliases' from 'C:\\Users\\ibm\\Anaconda3\\lib\\encodings\\aliases.py'>
# encodings.utf_8 : <module 'encodings.utf_8' from 'C:\\Users\\ibm\\Anaconda3\\lib\\encodings\\utf_8.py'>
# _signal : <module '_signal' (built-in)>
# __main__ : <module '__main__'>
- modules in sys.path
import sys
sys.path
# ['E:\\ipython',
# 'C:\\Users\\ibm\\Anaconda3\\python37.zip',
# 'C:\\Users\\ibm\\Anaconda3\\DLLs',
# 'C:\\Users\\ibm\\Anaconda3\\lib',
# 'C:\\Users\\ibm\\Anaconda3',
# '',
# 'C:\\Users\\ibm\\AppData\\Roaming\\Python\\Python37\\site-packages',
# 'C:\\Users\\ibm\\Anaconda3\\lib\\site-packages',
# 'C:\\Users\\ibm\\Anaconda3\\lib\\site-packages\\win32',
# 'C:\\Users\\ibm\\Anaconda3\\lib\\site-packages\\win32\\lib',
# 'C:\\Users\\ibm\\Anaconda3\\lib\\site-packages\\Pythonwin',
# 'C:\\Users\\ibm\\Anaconda3\\lib\\site-packages\\IPython\\extensions',
# 'C:\\Users\\ibm\\.ipython']
- The first path in sys.path is the folder where the current execution file is located
- If you need to import a module that is not in this folder, you need to add the module path to sys.path
import sys
sys.path.append("C:\\Users\\ibm\\Desktop") # Note that it is double slashes, if it is win
import fun3
fun3.f3() # Import fun3 successfully
Related readings
- Python Object Oriented Programming
- Python Cheatsheet
- Python Function Parameter
- Python Control Structure
- Python Composite Data Type
If you find this blog useful and want to support my blog, need my skill for something, or have a coffee chat with me, feel free to: