1. File reading and writing
1.1 Reading files
(1) Open the file
Open a file object using Python's built-in open()
function, passing in the filename and identifier:
>>> import os
>>> f = open('/Users/michael/test.txt', 'r')
The identifier 'r'
means read, so we have successfully opened a file.
Python introduces with
the statement to avoid program errors:
with open('/path/to/file', 'r') as f:
print(f.read())
The code above is equivalent to:
try:
f = open('/path/to/file', 'r')
print(f.read())
finally:
if f:
f.close()
(2) Read file content
method | effect |
---|---|
read() | Read the entire contents of a file at once |
read(size) | Read at most size bytes of content at a time |
readline() | Read one line at a time |
readlines() | Read all at once and return by row |
(3) Close the file
Call close()
the method to close the file. Files must be closed after use because file objects occupy operating system resources.
>>> f.close()
(4) Read other encoding format files
To read binary files, such as images, videos, etc., 'rb'
just open the file with a mode:
>>> f = open('/Users/michael/test.jpg', 'rb')
>>> f.read()
b'\xff\xd8\xff\xe1\x00\x18Exif\x00\x00...' # 十六进制表示的字节
To read non-UTF-8 encoded text files, you need to open()
pass parameters to the function encoding
, for example, to read GBK encoded files:
>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk')
>>> f.read()
'测试'
1.2 Writing files
To call open()
, pass an identifier 'w'
or 'wb'
indicate to write a text file or write a binary file:
>>> f = open('/Users/michael/test.txt', 'w')
>>> f.write('Hello, world!')
>>> f.close()
Can be called repeatedly write()
to write to the file, but must be called f.close()
to close the file. When we write a file, the operating system often does not write the data to the disk immediately, but caches it in memory, and writes it slowly when it is idle. Only close()
when the method is called, the operating system guarantees that all unwritten data is written to disk.
So usually, you still use with
the statement to write the file:
with open('/Users/michael/test.txt', 'w') as f:
f.write('Hello, world!')
2. File and directory operations
The built-in os
modules of Python can directly call the interface functions provided by the operating system.
2.1 Directory operations
(1) View the directory
# 查看当前目录的绝对路径:
>>> os.path.abspath('.')
'/Users/michael'
(2) Create a directory
# 在某个目录下创建一个新目录,首先把新目录的完整路径表示出来:
>>> os.path.join('/Users/michael', 'testdir')
'/Users/michael/testdir'
# 然后创建一个目录:
>>> os.mkdir('/Users/michael/testdir')
(3) Delete directory
# 删掉一个目录:
>>> os.rmdir('/Users/michael/testdir')
(4) Merge and split paths
When combining two paths into one, don't spell strings directly, but use os.path.join()
functions.
>>> os.path.join('/Users/michael', 'testdir')
'/Users/michael/testdir'
When splitting the path, don't split the string directly, but use os.path.split()
the function.
>>> os.path.split('/Users/michael/testdir/file.txt')
('/Users/michael/testdir', 'file.txt')
(5) Get the extension
os.path.splitext()
can directly get you the file extension:
>>> os.path.splitext('/path/to/file.txt')
('/path/to/file', '.txt')
2.2 File Operation
(1) File renaming
# 对文件重命名:
>>> os.rename('test.txt', 'test.py')
(2) File deletion
# 删掉文件:
>>> os.remove('test.py')
(3) Copy and move files
os
The function of copying and moving files is not provided in the module, but the function shutil
provided by the module copyfile()
can be regarded as os
a supplement to the module.
shutil.copy(src_file, target_path)
shutil.move(src_file, target_path)
import shutil
shutil.copy('/Users/michael/testdir1/file.txt', '/Users/michael/testdir2')
shutil.move('/Users/michael/testdir1/file.txt', '/Users/michael/testdir2')
3. JSON
To transfer objects between different programming languages, objects must be serialized into a standard format, most commonly serialized into JSON.
Because JSON is represented as a string, it can be read by all languages, and it can be easily stored on disk or transmitted over the network. Not only is JSON a standard format, but it is also faster than XML, and it can be read directly on a Web page, which is very convenient.
3.1 Python dict -> JSON
Python's built-in json
modules provide a very complete conversion of Python objects to JSON format. Let's first look at how to turn a Python object into a JSON:
>>> import json
>>> d = dict(name='Bob', age=20, score=88)
>>> json.dumps(d)
'{"age": 20, "score": 88, "name": "Bob"}'
dumps()
The method returns one str
, and the content is standard JSON.
dump()
method can directly write JSON to a file-like Object
.
3.2 JSON -> Python dict
To deserialize JSON into a Python object, use loads()
or the corresponding load()
method, the former deserializes the JSON string, and the latter reads the string file-like Object
from and deserializes it:
>>> json_str = '{"age": 20, "score": 88, "name": "Bob"}'
>>> json.loads(json_str)
{
'age': 20, 'score': 88, 'name': 'Bob'}
3.3 Python Class -> JSON
Since usually, we will use to class
represent the object, if the object is serialized directly, an error will be reported because Student
the object is not an object that can be serialized into JSON.
import json
class Student(object):
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
s = Student('Bob', 20, 88)
print(json.dumps(s))
# 输出
Traceback (most recent call last):
...
TypeError: <__main__.Student object at 0x10603cc50> is not JSON serializable
However, dumps()
the method also provides a large number of optional parameters, among which the optional parameters default
support serialization by providing conversion functions for dumps()
passing in object instances and conversion functions.
The simplified way of writing is as follows:
print(json.dumps(s, default=lambda obj: obj.__dict__))
Because the usual class
instance has an __dict__
attribute, which is one dict
, used to store instance variables.
3.4 JSON -> Python Class
If we want to deserialize JSON into an Student
object instance, loads()
the method first converts an dict
object, and then object_hook
the function we pass in is responsible for dict
converting it into Student
an instance:
def dict2student(d):
return Student(d['name'], d['age'], d['score'])
>>> json_str = '{"age": 20, "score": 88, "name": "Bob"}'
>>> print(json.loads(json_str, object_hook=dict2student))
<__main__.Student object at 0x10cd3c190>