1、编码格式介绍
常见的编码格式
- Python的解释器使用的是Unicode(内存)
- .py文件在磁盘上使用UTF-8存储(外存)
修 改编码格式#encoding = gbk
2、文件的读写原理
读写是根据py文件来说的
读:从file读(input) 写:py写到file(output)
- 文件的读写俗称“IO操作”
- 文件读写操作流程
- 操作原理
3、文件读写操作
-
内置函数open()创建文件对象
-
语法规则
file = open(filename[,mode,encoding])
被创建的对象 = open(要创建或打开的文件名[,打开模式默认只读,编码格式默认gbk])
文件类型
按照文件中数据的组织形式,文件分为以下两大类
- 文本文件:存储的是普通的”字符“文本,默认为unicode字符集,可以使用记事本程序打开
- 二进制文件:把数据内容用“字节”进行存储,无法用记事本打开,必须使用专用的软件打开,例如mp3,jpg格式等
打开模式 | 描述 |
---|---|
r | 以只读模式打开文件,文件的指针将会放在文件的开头 |
w | 以只写模式打开文件,如果文件不存在则创建,如果文件存在则覆盖原有内容,文件的指针将会放在文件的开头 |
a | 以追加模式打开文件,如果文件不存在则创建,文件指针在文件开头,如果文件存在,则在文件尾部追加内容,文件指针在原文件尾 |
b | 以二进制方式打开文件,不能单独使用,需要与其他模式一起使用;rb/wb |
+ | 以读写方式打开文件,不能单独使用,需要与其他模式一起使用;a+ |
file = open("1.txt","r")
print(file.read())
file.close()
# 复制附片
src_file = open("12.png", "rb")
target_file = open("12copy.png","wb")
target_file.write(src_file.read())
4、文件对象常用方法
方法名 | 说明 |
---|---|
read([size]) | 从文件中读取size个字节或字符的内容返回,若省略[size]则读取文件所有内容 |
readline() | 从文本文件中读取一行内容 |
readlines() | 把文本文件中每一行都作为独立的字符串对象,并将这些对象放入列表返回 |
write(str) | 将字符串str内容写入文件 |
writelines(s_list) | 将字符串列表s_list写入文本文件,不添加换行符 |
seek(offset[,whence]) | 把文件指针移动到新的位置,offset表示相对于whence的位置。offset为正则往结束方向,为负则往开始方向移动whence不同值代表不同含义:0:从文件头(默认)/1:当前位置/2:文件末尾位置 |
tell() | 返回文件指针的当前位置 |
flush() | 把缓冲区的内容写入文件,但不关闭文件 |
close() | 把缓冲区内容写入文件,同时关闭文件,释放文件对象相关资源 |
5、with语句(上下文管理器)
with语句可以自动管理上下文资源,不论什么原因跳出with快,都能确保文件正常关闭,以此来达到释放资源的目的
#要遵守上下文管理协议
#即要实现__enter__()方法和__exit__()方法
#上下文语法
with open('logo.png','rb') as src_file:
with语句体
#with语句体会会自动调用__enter__并将返回值赋给src_file
#离开运行上下文,自动调用上下文管理器的特殊方法__exit__()
#open('logo.png','rb')是上下文表达式,结果是上下文管理器
#利用with实现copy
with open('12.png','rb') as src_file:
with open('12copy.png','wb') as new_file:
new_file.write(src_file.read())
6、目录操作
os模块操作目录
- os模块是Python内置的与操作系统功能和文件系统相关的模块,该模块中的语句的执行结果通常与操作系统有关,在不同操作系统上运行,得到的结果可能不一样。
- os模块与os.path模块用于对目录或文件进行操作
函数 | 说明 |
---|---|
getcwd() | 返回当前的工作目录 |
lisdir(path) | 返回指定路径下的文件和目录信息 |
mkdir(path,[mode]) | 创建目录 |
makedirs(path1/path2...[,mode]) | 创建多级目录 |
rmdir(path) | 删除目录 |
removedirs(path1/path2...) | 删除多级目录 |
chdir(path) | 将path设置为当前工作目录 |
import os
print(os.name)
#os.system('open /Applications/QQ.app') #mac要加上open win不需要
#os.mkdir("newdir")
lis = os.listdir("../Demo02")
print(lis)
#os.makedirs('A/B/C')
#os.rmdir('newdir')
#os.removedirs('A/B/C')
print(os.getcwd()) #获取当前目录
os.chdir('/Users/gongjunpeng/IdeaProjects/Demopy/.idea/Demo0')#设置工作目录
print(os.getcwd())
os.path模块操作目录
函数 | 说明 |
---|---|
abspath(path) | 用于获取文件或目录的绝对路径 |
exists(path) | 用于判断文件或目录是否存在,返回True或False |
join(path,name) | 将目录与目录或文件名拼接起来 |
split() | 将文件和目录拆分 |
splitext() | 分离文件名和扩展名 |
basename(path) | 从一个目录中提取文件名 |
dirname(path) | 从一个路径中提取文件路径,不包括文件名 |
isdir(path) | 用于判断是否是路径 |
import os.path
filePath = os.path.abspath('../Demo02')
print(filePath)
print(os.path.exists('Demo02.py'),os.path.exists('Demo2.py'))
print(os.path.join('/C/gong','1.py'))
print(os.path.split(filePath))
print(os.path.splitext(filePath))
print(os.path.basename('/A/B/C/1.txt'))
print(os.path.dirname('/A/B/C/1.txt'))
print(os.path.isdir('/A/B/C/'))
walk方法
可以获得出所有文件的目录路径,文件名
import os
path = os.getcwd()
lst_files = os.walk(path)
for dirpath,dirname,filename in lst_files:
for file in dirname:
print(os.path.join(dirpath,file))
for file1 in filename:
print(os.path.join(dirpath,file1))
print('========')