1.文件的打开方式
打开文件的三个步骤: 打开 ---> 操作 ---> 关闭
三种主要的打开方式:
(1). r 模式
r:(默认)
-只能读,不能写
-读取文件不存在,会报错
[kiosk@foundation66 ~]$ su -
Password:
[root@foundation66 ~]# touch /mnt/westos
[root@foundation66 ~]# cd /mnt
[root@foundation66 mnt]# vim westos
[root@foundation66 mnt]# cat westos
hello python
[root@foundation66 mnt]# ll westos
-rw-r--r--. 1 0 root 13 Feb 9 14:38 westos
# 1.打开文件(默认可读)
f = open('/mnt/westos')
# 2.操作文件
# 读取文件
content = f.read()
# 打印文件内容
print(content)
# 写入文件内容,报错:因为文件打开方式只读
# f.write('hello')
# 查看文件是否可读,是否可写
print(f.readable())
print(f.writable())
# 3.关闭文件
f.close()
(2). w 模式
w:
-write only
-会清空文件之前的内容
-文件不存在,不会报错,会创建新的文件并写入
1)文件存在
# 1.由于系统文件本身没有权限,写入文件内容失败
# 只写方式打开文件
f = open('/mnt/westos','w')
# 写入文件内容,报错:Permission denied,由于系统文件本身没有权限
f.write('hello')
f.close()
# 给系统文件添加权限
[root@foundation66 mnt]# chmod 777 /mnt/westos
[root@foundation66 mnt]# ll westos
-rwxrwxrwx. 1 0 root 13 Feb 9 14:38 westos
# 2.添加文件权限后便不会报错,并且发现w方式会覆盖原文件内容
[root@foundation66 mnt]# cat /mnt/westos
hello
2)文件不存在
若文件不存在,则会先创建该文件,再写入文件内容
[root@foundation66 mnt]# ll /mnt/linux
ls: cannot access /mnt/linux: No such file or directory
f = open('/mnt/linux','w')
f.write('hello linux' )
f.close()
[root@foundation66 mnt]# cat /mnt/linux
hello linux
(3). a 模式
a:
-write only
-不会清空文件内容
-文件不存在,不会报错,会创建新的文件并写入
1). 文件存在
[root@foundation66 mnt]# cat /mnt/westos
hello
# 不会覆盖原文件内容
f = open('/mnt/westos','a')
f.write('my python')
f.close()
[root@foundation66 mnt]# cat /mnt/westos
hellomy python
2). 文件不存在
若文件不存在,则会先创建该文件,再写入文件内容
[root@foundation66 mnt]# ll /mnt/redhat
ls: cannot access /mnt/redhat: No such file or directory
f = open('/mnt/redhat','a')
f.write('my love')
f.close()
[root@foundation66 mnt]# cat /mnt/redhat
my love
另外的3种打开方式:
r+:
-可读写(rw)
-读取文件不存在,会报错
w+:
-可读写(rw)
-会清空原文件内容
-文件不存在,不报错,会创建新的文件
a+:
-可读写(rw)
-不会清空原文件内容
-文件不存在,不报错,会创建新的文件
2.文件的读取操作
[root@foundation66 tmp]# cp /etc/passwd /tmp
[root@foundation66 tmp]# chmod 777 /tmp/passwd
# 输出前4行
[root@foundation66 tmp]# head -n 4 /tmp/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@foundation66 tmp]# head -4 /tmp/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
# 输出前4个字符
[root@foundation66 tmp]# head -c 4 /tmp/passwd
root
# b: 二进制文件 r:只读
f = open('/tmp/passwd','rb')
# 1.文件内容自动输出到了一行
print(f.read())
f.close()
# b: 二进制文件 r:只读
f = open('/tmp/passwd','rb')
# 2.只读取一行(默认为第一行)
print(f.readline())
f.close()
# b: 二进制文件 r:只读
f = open('/tmp/passwd','rb')
# 3.读取文件内容,返回一个列表,列表的元素分别为文件的每行内容
print(f.readlines())
f.close()
# b: 二进制文件 r:只读
f = open('/tmp/passwd','rb')
# 4.输出文件的前4个字符;类似于head -c 4 /tmp/passwd
print(f.read(4))
f.close()
# 5.以列表的形式输出:去掉换行后的文件内容
# 方法1:(列表生成式) strip:去掉空格,换行也算作空格
# print([line.strip() for line in f.readlines()])
# # 方法2:(map内置高阶函数) lambda:匿名函数
# print(list(map(lambda x:x.strip(),f.readlines())))
3.文件指针
读取文件一般从'文件指针'位置开始读取
seek方法,移动指针
seek第一个参数是偏移量:>0代表向右移动,<0代表向左移动
seek第二个参数是:
0:移动指针到文件开头
1:不移动指针
2:移动指针到末尾
(1).读取文件一般从’文件指针’位置开始读取
# 文件指针:读取文件一般从文件指针位置开始读取
f = open('/tmp/passwd','r+')
# 读取文件
content = f.read()
# 打印文件内容
print(content)
# 打印文件指针
print(f.tell())
# 再次读取文件并打印
# 发现此时无法输出文件内容,因为此时文件指针已经指到文件末尾,
# 而文件的读取是从文件指针开始读取的
print(f.read())
# 打印文件指针
print(f.tell())
f.close()
(2)seek方法:移动指针
# 只读打开二进制文件
f = open('/tmp/passwd','rb')
# 1.查看当前指针所在位置
print(f.tell())
print(f.read(3))
print(f.tell())
# 2.将指针移动到倒数第二个字符位置处(文件末尾再向左移动一个字符)
f.seek(-1,2)
print(f.tell())
# 关闭文件
f.close()
4.非纯文本文件的读取
先将1111.jpg图片移动到该python文件路径下
路径: /home/kiosk/PycharmProjects/python/python08/01_文件
# 只读打开文件: mode表明打开的是文件或音频
f1 = open('1111.jpg',mode='rb')
# 读取文件
content = f1.read()
# 关闭文件
f1.close()
# 只写打开文件:westos.jpg图片不存在
f2 = open('westos.jpg',mode='wb')
# 将1111.jpg图片写入westos.jpg图片中
f2.write(content)
# 关闭文件
f2.close()
# 此时发现该路径下生成了(与1111.jpg相同的)图片westos.jpg图片
5.上下文管理器
上下文管理器:打开文件,执行完with语句内容之后,会自动关闭文件对象
备份/tmp/passwd文件内容到/tmp/passwdbackup文件中:
# 同时打开两个文件对象, \表示换行
with open('/tmp/passwd') as f1, \
open('/tmp/passwdbackup','w+') as f2:
# 将第一个文件的内容写入到第二个文件中
f2.write(f1.read())
# 发现此时文件指针在文件末尾
print(f2.tell())
# 由于读取文件是从文件指针位置开始读取的
# 所以需要先将指针移动到文件开头
f2.seek(0)
print(f2.tell())
# 读取文件内容
print(f2.read())
6.文件的综合练习
需求:
创建文件data.txt,文件共100000行,每行存放一个1~100之间的整数,写完后读取文件内容
代码:
import random
# 用with的方式打开文件,系统会自动关闭文件,即无需再执行f.close()
# a+:可读写,若打开的文件不存,则系统会自动创建
with open('data.txt','a+') as f:
for i in range(100000):
# 写入文件的内容必须是字符串类型
# \n 表示换行,每写入一行数字都需要换行
f.write(str(random.randint(1,101)) + '\n')
# 如果不将指针移动到文件开头,此时将读取不到任何文件内容
f.seek(0)
# 读取文件
print(f.read())
运行结果: