python corrupt extra field_Python——Input and Output

本文详细介绍了文件对象的方法,包括f.read()用于读取文件内容,f.readline()逐行读取,以及f.write()写入字符串。通过f.seek()可以改变文件指针位置,f.tell()返回当前位置。对于读取所有行,可以使用循环遍历文件对象或使用f.readlines()。这些方法在处理文件时提供了灵活性和效率。
摘要由CSDN通过智能技术生成

7.2.1. Methods of File Objects

The rest of the examples in this section will assume that a file object called f has already been created.

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ('').

>>>

>>> f.read()

'This is the entire file.\n'

>>> f.read()

''

f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\n', a string containing only a single newline.

>>>

>>> f.readline()

'This is the first line of the file.\n'

>>> f.readline()

'Second line of the file\n'

>>> f.readline()

''

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:

>>>

>>> for line in f:

... print(line, end='')

...

This is the first line of the file.

Second line of the file

If you want to read all the lines of a file in a list you can also use list(f) or f.readlines().

f.write(string) writes the contents of string to the file, returning the number of characters written.

>>>

>>> f.write('This is a test\n')

15

Other types of objects need to be converted – either to a string (in text mode) or a bytes object (in binary mode) – before writing them:

>>>

>>> value = ('the answer', 42)

>>> s = str(value) # convert the tuple to string

>>> f.write(s)

18

f.tell() returns an integer giving the file object’s current position in the file represented as number of bytes from the beginning of the file when in binary mode and an opaque number when in text mode.

To change the file object’s position, use f.seek(offset,from_what). The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.

>>>

>>> f = open('workfile', 'rb+')

>>> f.write(b'0123456789abcdef')

16

>>> f.seek(5) # Go to the 6th byte in the file

5

>>> f.read(1)

b'5'

>>> f.seek(-3, 2) # Go to the 3rd byte before the end

13

>>> f.read(1)

b'd'

In text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)) and the only valid offset values are those returned from the f.tell(), or zero. Any other offset value produces undefined behaviour.

File objects have some additional methods, such as isatty() and truncate() which are less frequently used; consult the Library Reference for a complete guide to file objects.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值