python的print函数中file_python中file对象的常用方法

### open() 方法

Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。(使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法)

```

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,

closefd=True, opener=None)

```

- file:必需,文件路径(相对或者绝对路径)。

- mode: 可选,文件打开模式

- buffering: 设置缓冲

- encoding: 一般使用utf8

- errors:报错级别

- newline: 区分换行符

- closefd:传入的file参数类型

### file对象的常用函数

#### close()方法

close()方法用于关闭一个已打开的文件,关闭后的文件不能再进行读写操作, 否则会触发 ValueError 错误。close() 方法允许调用多次。

```

语法:fileObject.close();

```

```

file = open("hello.txt","wb")

print(file.name)

file.close()

# 输出:hello.txt

```

#### read() 方法

read() 方法用于从文件读取指定的字节数,如果未给定或为负则读取所有。

首先自己创建一个hello.txt文档(自定义),文档中的内容是hello world。

```

file = open("hello.txt", "r")

read1 = file.read()

print(read1)

file.close()

# 输出:hello world

```

#### write()方法

write()方法用于向文件中写入指定字符串。

向一个空的hello.txt文档中写入hello world。

```

file = open("hello.txt", "w")

file.write("hello world")

file.close()

```

#### writelines() 方法

writelines() 方法用于向文件中写入一序列的字符串。这一序列字符串可以是由迭代对象产生的,如一个字符串列表。换行需要制定换行符 \n。

```

file = open("hello.txt", "w")

con = ["a \n", "b\n", "c\n"]

file.writelines(con)

file.close()

# hello.txt文件中写入

a

b

c

```

#### flush() 方法

flush() 方法是用来刷新缓冲区的,即将缓冲区中的数据立刻写入文件,同时清空缓冲区,不需要是被动的等待输出缓冲区写入。

一般情况下,文件关闭后会自动刷新缓冲区,但如果你需要在关闭前刷新它,就可以使用 flush() 方法。

```

file = open("hello.txt", "wb")

print("文件名:", file.name)

file.flush()

file.close()

# 文件名: hello.txt

```

#### readline() 方法

readline() 方法用于从文件读取整行,包括 "\n" 字符。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 "\n" 字符。

```

# hello.txt的内容

123

456

789

```

```

file = open("hello.txt", "r")

content = file.readline()

print(content)

file.close()

# 输出hello.txt文件的第一行:123

```

#### readlines() 方法

readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for... in ... 结构进行处理。如果碰到结束符 EOF 则返回空字符串。

```

file = open("hello.txt", "r")

content = file.readlines()

print(content)

file.close()

# 输出:['123\n', '456\n', '789']

```

#### seek() 方法

seek() 方法用于移动文件读取指针到指定位置。

```

fileObject.seek(offset[, whence])

```

- offset表示开始的偏移量,也就是代表需要移动偏移的字节数。

- whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。

假设hello.txt文件中的内容是abcdefghijk,那么我们使用 seek() 方法来移动文件指针试试:

```

file = open("hello.txt", "r")

file.seek(3) #文件指针移动到第三位,从第四位开始读

print(file.read()) # 输出:defghijk

file.seek(5)

print(file.read()) # 输出:fghijk

file.close()

```

#### tell() 方法

tell() 方法返回文件的当前位置,即文件指针当前位置。

```

file = open("hello.txt", "r")

file.seek(4) #文件指针移动到第三位,从第四位开始读

print(file.tell()) # 4

file.close()

```

#### fileno()方法

fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作。

```

file = open("hello.txt", "w")

con = file.fileno()

print(con)

file.close()

# 输出:3

```

#### isatty()方法

如果文件连接到一个终端设备返回 True,否则返回 False。

```

file = open("hello.txt", "w")

con = file.isatty()

print(con)

file.close()

# 输出:False

```

#### truncate() 方法

truncate() 方法用于截断文件,如果指定了可选参数 size,则表示截断文件为 size 个字符。 如果没有指定 size,则从当前位置起截断;截断之后 size 后面的所有字符被删除。

```

# hello.txt文本

a

b

c

d

e

```

```

file = open("hello.txt", "r")

con = file.readline()

print(con) # 输出:a

file.truncate() # 截断剩下的字符串

con = file.readline()

print(con)

file.close()

```

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69908432/viewspace-2653360/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值