一、Python控制文件的三步
- 1、打开
- 2、操作
- 3、关闭
示例:
Linux环境下,我们先把/etc/passwd 复制到/tmp
代码示例:
#以只读方式打开文件
f = open('/tmp/passwd','r')
#读操作
content = f.read()
print(content)
print(f.writable()) #查看对于f是否可写
print(f.readable()) #查看对于f是否可读
f.close()
执行效果:
二、文件的读写操作权限
例如:
f = open('/tmp/passwd','r') #仅读方式
那么都有什么操作权限呢?
权限 | 作用 |
---|---|
r:(默认) | 只能读,不能写。读取的文件不存在,会报错 |
r+ | 可以执行读写操作,文件不存在,报错。默认情况下,从文件指针所在位置开始写入 |
w | write only,会清空文件之前的内容。文件不存在,不会报错,会创建新的文件并写入 |
w+ | rw,会清空文件内容。文件不存在,不报错,会创建新的文件 |
a | write only,不会清空文件内容。文件不存在,会报错 |
a+ | rw,不清空文件内容,在末尾追加。文件不存在,不报错 |
示例:
f = open('/tmp/passwd','r+') #以r+的方式打开文件
print(f.tell()) #查看当前指针所在的位置
f.write('python')
print(f.tell()) #查看写入字符后的文件指针位置
content = f.read()
print(content)
f.close()
执行效果:
分析: 可以看到没有写入之前指针在文件的开头,写入python后,文件位置在n后面,因此读取文件内容时没有显示python字样。那么怎么恢复指针位置呢?
f.seek(0) #将指针位置移动到文件爱你开头
添加这个代码后再读取内容:
f = open('/tmp/passwd','r+')
#查看当前指针所在的位置
print(f.tell())
f.write('python')
f.seek(0)
print(f.tell())
content = f.read()
print(content)
f.close()
三、文件的读取操作
- .read() 一次读取文件所有内容
- .readline() 返回文件第一行的内容作为一个元素
- .readlines() 读取文件内容,返回一个列表,列表的元素分别为文件
行内容
默认情况下读取文件的所有内容,小文件可以直接用read读取,如果
是大文件(文件大小>内存大小),不能通过read一次性读取所有内容
1、.read()
示例:
f = open('/tmp/passwd','r+')
content = f.read() #读取文件所有内容
print(content)
f.close()
在括号内加入数字可以按字节读取:
f = open('/tmp/passwd','r+')
content = f.read(6)
print(content)
f.close()
2、.readline()
示例:
f = open('/tmp/passwd','r+')
content = f.readline()
print(content)
f.close()
执行效果:
3、.readlines()
示例:
f = open('/tmp/passwd','r+')
content = f.readlines()
print(content)
f.close()
执行效果:
四、文件的写入操作
f.write('hello') #将指定字符写入文件
f.writelines(['a','b']) #将列表里的每个元素写入文件
示例:
f = open('/tmp/passwd','r+')
f.writelines(['a','b']) #将列表里的每个元素写入文件
f.seek(0)
print(f.read())
执行效果:
五、移动文件指针
seek方法,移动指针
seek的第一个参数是偏移量:即偏移多少
seek的第二个参数是:
0:移动指针从文件开头开始
1:不移动指针,即当前位置
2:移动指针从末尾开始
示例:
f = open('/tmp/passwd','w+')
f.write('hello world')
print(f.tell()) #查看文件指针位置
print(f.read()) #读取文件内容
f.seek(0) #更改文件指针到文件开头
print(f.tell()) #查看文件指针位置
print(f.read()) #重新查看文件内容
f.close()
六、非纯文本文件读取
读取文本文件:
r r+ w w+ a a+
读取二进制文件:
rb rb+ wb wb+ ab ab+
示例:
f1 = open('hello.png',mode='rb')
content = f1.read()
f1.close()
f2 = open('121.jpg',mode='wb')
f2.write(content)
f2.close()
执行效果:
七、上下文管理器
本章刚开始,我们说过,文件操作分为三步:
- 1、打开
- 2、操作
- 3、关闭
我们在编写代码时,很容易忘记第三步,因此我们用到上下文管理器,我们举个例子说明怎么使用?
f = open('/tmp/passwd')
with open('/tmp/passwd') as f: #用with as 打开文件,会再操作后,自动关闭
print(f.read())
示例:
#同时打开两个文件
with open('/tmp/passwd') as f1,\
open('/tmp/passwd1','w+') as f2:
#将第一个文件的内容写入第二个文件中
f2.write(f1.read())
#移动指针到文件最开始
f2.seek(0)
#读取文件内容
print(f2.read())
八、查看系统
1、返回操作系统类型
import os
print(os.name) #值为:posix,表示linux操作系统,如果是nt,是windows操作系统
2、操作系统的详细信息
import os
info = os.uname()
print(info)
print(info.sysname) #查询操作系统类型
print(info.nodename) #查询主机名
3、系统环境变量
import os
print(os.environ)
4、通过key值获取环境变量对应的value值
import os
print(os.environ.get('PATH'))
九、对文件名和路径的操作
1、判断是否为绝对路径
import os
print(os.path.isabs('/tmp/westos'))
print(os.path.isabs('data.txt'))
2、生成绝对路径
import os
print(os.path.abspath('hello.png'))
print(os.path.join('/home/kiosk','hello.png'))
print(os.path.join(os.path.abspath('.'),'hello.jpg'))
3、获取目录名或文件名
filename = '/home/kiosk/PycharmProjects/20190316/day07/hello.png'
print(os.path.basename(filename))
print(os.path.dirname(filename))
4、创建目录、删除目录
os.mkdir('img') #建立目录
os.makedirs('img1/jpg/png') #递归建立目录
os.rmdir('img') #删除一个空目录
5、创建文件、删除文件
os.mknod('aa.txt')
os.remove('aa.txt')
6、文件重命名
os.rename('data.txt','data1.txt') #将data.txt改为data1.txt
7、判断文件或者目录是否存在
print(os.path.exists('data1.txt'))
8、分离后缀名和文件名
print(os.path.splitext('hello.png'))
9、将目录名和文件名分离
print(os.path.split('/tmp/hello/python.jpg'))
十、遍历目录
示例:
import os
from os.path import join
for root,dir,files in os.walk('/var/log'):
print(root) #正在遍历的目录的本身的地址
print(dir) #一个列表,元素为该目录下所有的目录的名称
print(files) #一个列表,元素是该目录下所有的文件名
for name in files:
print(join(root,name)) #组合为目录下所有的文件的绝对路径
执行效果: