Python Data Persistence 简明教程
File Handling with os Module
除了 open() 函数返回的文件对象,还可以使用 Python 内置库的 os 模块执行文件 IO 操作,该模块提供了实用的操作系统相关函数。这些函数对文件执行低级别的读/写操作。
In addition to File object returned by open() function, file IO operations can also be performed using Python’s built-in library has os module that provides useful operating system dependent functions. These functions perform low level read/write operations on file.
os 模块中的 open() 函数类似于内置 open()。但是,它不返回文件对象,而是返回文件描述符,它是一个与打开的文件相对应的唯一整数。文件描述符的值 0、1 和 2 分别表示标准输入、标准输出和标准错误流。其他文件将从 2 开始获得递增的文件描述符。
The open() function from os module is similar to the built-in open(). However, it doesn’t return a file object but a file descriptor, a unique integer corresponding to file opened. File descriptor’s values 0, 1 and 2 represent stdin, stdout, and stderr streams. Other files will be given incremental file descriptor from 2 onwards.
与 open() 内置函数的情况类似, os.open() 函数也需要指定文件访问模式。下表列出 os 模块中定义的各种模式。
As in case of open() built-in function, os.open() function also needs to specify file access mode. Following table lists various modes as defined in os module.
Sr.No. |
Os Module & Description |
1 |
os.O_RDONLY Open for reading only |
2 |
os.O_WRONLY Open for writing only |
3 |
os.O_RDWR Open for reading and writing |
4 |
os.O_NONBLOCK Do not block on open |
5 |
os.O_APPEND Append on each write |
6 |
os.O_CREAT Create file if it does not exist |
7 |
os.O_TRUNC Truncate size to 0 |
8 |
os.O_EXCL Error if create and file exists |
若要打开一个新文件以写入数据,请通过插入管道 (|) 运算符指定 O_WRONLY 和 O_CREAT 模式。os.open() 函数返回文件描述符。
To open a new file for writing data in it, specify O_WRONLY as well as O_CREAT modes by inserting pipe (|) operator. The os.open() function returns a file descriptor.
f=os.open("test.dat", os.O_WRONLY|os.O_CREAT)
请注意,数据是以字节字符串的形式写入磁盘文件的。因此,通过使用 encode() 函数将普通字符串转换成字节字符串,如同之前一样。
Note that, data is written to disk file in the form of byte string. Hence, a normal string is converted to byte string by using encode() function as earlier.
data="Hello World".encode('utf-8')
os 模块中的 write() 函数接受此字节字符串和文件描述符。
The write() function in os module accepts this byte string and file descriptor.
os.write(f,data)
别忘了使用 close() 函数关闭文件。
Don’t forget to close the file using close() function.
os.close(f)
要使用 os.read() 函数读取文件的内容,请使用以下语句:
To read contents of a file using os.read() function, use following statements:
f=os.open("test.dat", os.O_RDONLY)
data=os.read(f,20)
print (data.decode('utf-8'))
请注意,os.read() 函数需要文件描述符和要读取的字节数(字节字符串的长度)。
Note that, the os.read() function needs file descriptor and number of bytes to be read (length of byte string).
如果要同时打开一个文件进行读/写操作,请使用 O_RDWR 模式。下表显示了 os 模块中与文件操作相关的重要函数。
If you want to open a file for simultaneous read/write operations, use O_RDWR mode. Following table shows important file operation related functions in os module.
Sr.No |
Functions & Description |
1 |
os.close(fd) Close the file descriptor. |
2 |
os.open(file, flags[, mode]) Open the file and set various flags according to flags and possibly its mode according to mode. |
3 |
os.read(fd, n) Read at most n bytes from file descriptor fd. Return a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned. |
4 |
os.write(fd, str) Write the string str to file descriptor fd. Return the number of bytes actually written. |