文章目录
前言
本文为7月6日Python基础学习笔记,分为十二个章节:
- file 文件操作:创建文件对象、文本文件的写入;
- 编码常识;
- with 上下文管理;
- 文本文件的读取:read()、readline()、readlines();
- 二进制文件的读取&写入;
- 文件对象的常用属性和方法;
- seek() 任意位置操作;
- pickle 实现序列化&反序列化;
- .csv 文件的读取和写入;
- os 模块:调用操作系统可执行文件、获得文件信息、创建文件夹、递归创建、os.path 模块、walk 遍历;
- shutil 模块:文件和目录拷贝、zipfile 模块——压缩&解压;
- 递归展示目录树结构。
一、file 文件操作
-
分类:
- 文本文件: 存储普通字符文本,python 默认为 unicode 字符集;
- 二进制文件: 二进制文件把数据内容用字节进行存储,无法用记事本打开。必须使用专用的软件解码。
-
文件操作相关模块:
1、创建文件对象 open()
- 示例:
f = open(r"d:\b.txt", "w")
- 打开方式:
2、文本文件的写入
#coding=utf-8
# 文本文件的写入
f = open(r"a.txt", "a")
s = "xiaoli\n 18\n"
f.write(s)
f.close()
>>> xiaoli
>>> 18
3、write() & writelines()
- write(a):把字符串 a 写入到文件中;
- writelines():把字符串列表写入文件中,不添加换行符;
#coding=utf-8
f = open(r"a.txt", "w")
s = ["小李\n", "中李\n", "大李\n"]
f.writelines(s)
f.close()
>>> 小李
>>> 中李
>>> 大李
二、编码常识
- 常用编码之间的关系:
三、with 上下文管理
with 关键字可以自动管理上下文资源,不论什么原因跳出 with 块,都能确保文件正确的关闭, 并且可以在代码块执行完毕后自动还原进入该代码块时的现场。
#coding=utf-8
# 使用 with 管理文件写入
s = ["小李\n", "中李\n", "大李\n"]
with open(r"a.txt", "w") as f:
f.writelines(s)
>>> 小李
>>> 中李
>>> 大李
四、文本文件的读取
1、read([size])
- 从文件中读取 size 个字符,并作为结果返回。如果没有 size 参数,则读取整个文件;
- 读取到文件末尾,会返回空字符串。
2、readline()
-
读取一行内容作为结果返回。读取到文件末尾,会返回空字符串。
-
按行读取一个文件:
with open(r"a.txt", "r") as f:
while True:
fragment = f.readline()
if not fragment: # 如果是空字符串
break
else:
print(fragment, end="")
- 使用迭代器(每次返回一行)读取文本文件:
with open(r"a.txt", "r") as f:
for a in f:
print(a, end="")
- 为文本文件每一行的末尾增加行号:
#coding=utf-8
with open(r"a.txt", "r") as f:
lines = f.readlines()
lines = [line.rstrip() + "#" + str(index+1) + "\n" for \
index, line in enumerate(lines)]
'''.rstrip: 去除字符串右侧指定字符;
enumerate(): 将集合作为枚举对象返回
["第一行", "第二行", "第三行"] --> [(0, '第一行'), (1, '第二行'), (2, '第三行')]'''
with open("a.txt", "w", encoding="utf-8") as f:
f.writelines(lines)
>>> 第一行#1
>>> 第二行#2
>>> 第三行#3
3、readlines()
- 文本文件中,每一行作为一个字符串存入列表中,返回该列表。
五、二进制文件的读取&写入
- 读取图片文件,实现文件的拷贝:
with open('a.jpg', 'rb') as f:
with open('a_copy', 'wb') as w:
for line in f.readlines():
w.write(line)
print("拷贝完成")
六、文件对象的常用属性和方法
- 文件对象的属性:
- 文件对象的打开模式:
- 文件对象的常用方法:
七、seek() 任意位置操作
- seek()移动文件指针示例:
#coding=utf-8
with open("a.txt", "r") as f:
print("文件名:{0}".format(f.name))
print(f.tell())
print("读取的内容:{0}".format(str(f.readline())))
print(f.tell())
f.seek(9, 0)
print("读取的内容:{0}".format(str(f.readline())))
>>> 文件名:a.txt
>>> 0
>>> 读取的内容:abcdefg
>>> 9
>>> 读取的内容:
八、pickle 实现序列化&反序列化
-
序列化: 将对象转化成“串行化”数据形式, 存储到硬盘或通过网络传输到其他地方;
-
反序列化: 相反的过程,将读取到的“串行化数据”转化成对象。
-
将对象序列化到文件中:
#coding=utf-8
import pickle
with open(r"data.txt", "wb") as f:
a1 = "小李"
a2 = 123
a3 = [1, 2, 3]
pickle.dump(a1, f)
pickle.dump(a2, f)
pickle.dump(a3, f)
- 将获得的数据反序列化成对象:
#coding=utf-8
with open(r"data.txt", "rb") as f:
a1 = pickle.load(f)
a2 = pickle.load(f)
a3 = pickle.load(f)
print(a1)
print(a2)
print(a3)
>>> 小李
123
[1, 2, 3]
九、.csv 文件的读取和写入
1、csv.reader 对象和 csv 文件读取
import csv
with open(r"D:\learning_materials\Python_fundations\a.csv", "r", encoding='UTF-8') as a:
a_csv = csv.reader(a) # 创建 csv 对象(列表),包含所有数据,每一行为一个元素
headers = next(a_csv) # 得列表对象,包含标题行的信
print(headers)
for row in a_csv:
print(row)
>>> ['\ufeff姓名', '年龄', '工作', '薪水']
['小李', '18', '工人', '20000']
['中李', '19', '农民', '20000']
['大李', '20', '资本家', '1000']
2、csv.writer 对象和 csv 文件写入
headers = ["工号","姓名","年龄","地址","月薪"]
rows = [("1001","高淇",18,"西三旗 1 号院","50000"),("1002","高八",19,"西三旗 1 号院","3000")]
with open(r"D:\learning_materials\Python_fundations\a.csv", "w", encoding='UTF-8') as b:
b_csv = csv.writer(b) # 创建 csv 对象
b_csv.writerow(headers) # 写入一行标题
b_csv.writerow(rows) # 写入多行数据
十、os 模块
1、调用操作系统可执行文件
- 调用 windows 系统的记事本:
import os
os.system("notepad.exe")
2、获得文件信息、创建文件夹、递归创建
- 常用操作文件的方法:
3、os.path 模块
- 目录相关(路径判断、路径切分、路径连接、文件夹遍历)的操作:
- 测试 os.path 中常用方法:
#coding=utf-8
#对路径进行分割、连接操作
import os
import os.path
path = os.path.abspath("b.txt") # 返回绝对路径
print(os.path.split(path)) # 返回元组:目录、文件
print(os.path.splitext(path)) # 返回元组:路径、扩展名
print(os.path.join("aa", "bb", "cc")) # 返回路径:aa/bb/cc
>>> D:/learning_materials/jupyternotebook_projects/mypro_file/test_os/test_os.path.py
>>> ('D:\\learning_materials\\jupyternotebook_projects\\mypro_file\\test_os', 'b.txt')
>>> ('D:\\learning_materials\\jupyternotebook_projects\\mypro_file\\test_os\\b', '.txt')
aa\bb\cc
- 列出指定目录下所有的.py 文件,并输出文件名:
#coding=utf-8
import os
path = os.getcwd() # 返回当前工作目录
file_list = os.listdir(path) # 列出指定文件夹的子目录、子文件,返回的是列表
for filename in file_list:
if filename.endswith("py"):
print(filename)
print("#################")
file_list2 = [filename for filename in os.listdir(path) if filename.endswith("py")]
for f in file_list2:
print(f, end="\t")
>>> test_01.py
test_02.py
test_03.py
test_04.py
test_05.py
test_os.path.py
>>> #################
>>> test_01.py test_02.py test_03.py test_04.py test_05.py test_os.path.py
4、walk 遍历
返回一个 3 个元素的元组:
- dirpath: 列出指定目录的路径;
- dirnames: 目录下的所有文件夹;
- filenames: 目录下的所有文件。
使用 walk()递归遍历所有文件和目录:
#coding=utf-8
import os
all_files = []
path = os.getcwd() # 返回当前工作目录
list_file = os.walk(path) # 返回一个3个元素的元组
for dirpath, dirnames, filenames in list_file:
for dir in dirnames:
all_files.append(os.path.join(dirpath, dir))
for file in filenames:
all_files.append(os.path.join(dirpath, file))
for file in all_files:
print(file)
>>> D:\learning_materials\jupyternotebook_projects\mypro_file\test_os\b.txt
D:\learning_materials\jupyternotebook_projects\mypro_file\test_os\test_01.py
D:\learning_materials\jupyternotebook_projects\mypro_file\test_os\test_02.py
D:\learning_materials\jupyternotebook_projects\mypro_file\test_os\test_03.py
D:\learning_materials\jupyternotebook_projects\mypro_file\test_os\test_04.py
D:\learning_materials\jupyternotebook_projects\mypro_file\test_os\test_05.py
D:\learning_materials\jupyternotebook_projects\mypro_file\test_os\test_os.path.py
十一、shutil 模块
1、文件和目录拷贝
- 实现文件的拷贝:
#coding=utf-8
import shutil
shutil.copyfile("1.txt", "1_copy.txt")
- 实现递归的拷贝文件夹内容:
#coding=utf-8
import shutil
# “音乐”文件夹不存在才能使用
shutil.copytree("music/香港", "音乐", ignore=shutil.ignore_patterns("*.html", "*htm"))
2、zipfile 模块——压缩&解压
- 实现将文件夹所有内容压缩:
#coding=utf-8
import shutil
import zipfile
#shutil.make_archive("音乐/yinyue", "zip", "music/香港")
#z1 = zipfile.ZipFile("a.zip", "w")
#z1.close()
z2 = zipfile.ZipFile("a.zip", "r")
z2.extractall("音乐")
z2.close()
十二、递归展示目录树结构
- 使用递归算法遍历目录下所有文件:
#coding=utf-8
import os
allfiles = []
def getAllfiles(path, level):
childFiles = os.listdir(path) # 返回指定文件夹包含的文件名的列表
for file in childFiles:
filepath = os.path.join(path, file)
if os.path.isdir(filepath):
getAllfiles(filepath, level + 1)
allfiles.append("\t" * level + filepath)
getAllfiles("shutil", 0)
for f in reversed(allfiles):
print(f)