Python对文件的读写操作

Python之文件读写操作

文件:对文件进行增删改查

处理步骤:打开-操作-关闭

作用:长期可以保存数据如:(办公,娱乐等等)

文本用途:保存数据,配置文件,日志…

一、打开文件的方式

手动挡:指的是我们手动打开一个文件,然后对文件进行操作,最后我们关闭文件并保存

open+close: open && close 打开-关闭

自动挡:指的是程序可以自动关闭,不需要人工去关闭

with ,自动关闭

常见的操作

w, r, a, b

write

readline

readlines

seek

tell

read

相关的模块

fnmatch

glob

pickle

StringIO

shelve

1.1、手动挡

file = open("data.txt","w") # file是一个变量-->open 打开 data.txt这个文件以写入的方式打开

file.write("My Name Is ZhangSan") # file.write 在data.txt 中写入My Name Is ZhangSan

file.close() # 最后关闭此文件

文件内容为: My Name Is ZhangSan

1.2、自动挡

读:

with open("data.txt","r") as f: # with open 以读取的方式打开data.txt 文件给一个别名叫做f

data_result = f.readline() # data_result 是一个变量通过f 别名使用readline() 方法读取第一行赋值给变量data_result

print(data_result) # 最后print 输出data_result的内容

>>> My Name Is ZhangSan

1.3、如何追加增加数据?

with open("data.txt","a") as f: # with open 打开data.txt 这个文件以追加的方式 as f 别名为f

f.write("zhangsan1

") # 第一行写入zhangsan1

换行

f.write("zhangsan2

") # 第二行写入zhangsan2

换行以此类推

f.write("zhangsan3

")

f.write("zhangsan4

")

f.write("zhangsan5

")

验证追加结果:

with open("data.txt","r") as f: # with open 打开data.txt 文件以读取的方式打开别名为f

print(f.readline()) # 一行一行的输出

print(f.readline())

print(f.readline())

print(f.readline())

print(f.readline())

>>>python

My Name Is ZhangSanzhangsan1

zhangsan2

zhangsan3

zhangsan4

zhangsan5

1.4、readline 和 readline 区别?

with open("data.txt","r") as f:

result = f.readline()

print(f"这是readline输出的结果---->{result}")

with open("data.txt","r") as f:

data_result = f.readlines()

print(f"这是readlines输出的结果---->{data_result}")

>>>这是readline输出的结果---->My Name Is ZhangSanzhangsan1

>>>这是readlines输出的结果---->['My Name Is ZhangSanzhangsan1

', 'zhangsan2

', 'zhangsan3

', 'zhangsan4

', 'zhangsan5

']

1.5、tell 和 seek 区别?

file = open("data.txt")

print(file.tell())

print(file.readline())

print(file.tell())

print(file.readline())

print(file.tell())

print(file.readline())

file.seek(0)

print(file.tell())

print(file.readline())

file.seek(0)

print(f"这是第0个位置{file.readline()}")

>>>

0

My Name Is ZhangSanzhangsan1

30

zhangsan2

41

zhangsan3

0

My Name Is ZhangSanzhangsan1

这是第0个位置My Name Is ZhangSanzhangsan1

二、常见的模块介绍:

2.1、fnmatch

匹配后缀名文件

import os

import fnmatch

path_test = r"F:python_test1Yankerp_test

esult_test"

for f in os.listdir(path_test):

if fnmatch.fnmatch(f, "*.txt"):

print(f)

>>>

0.txt

1.txt

2.txt

3.txt

4.txt

aaa.txt

yankerp.txt

例如2:

import os

import fnmatch

path_test = r"F:python_test1Yankerp_test

esult_test"

for f in os.listdir(path_test):

if fnmatch.fnmatch(f, "*.doc"):

print(f)

>>>

0.doc

0l4Vx.doc

1.doc

2.doc

3.doc

4.doc

8A5ui.doc

bCy0P.doc

DLPK1.doc

KrHiC.doc

n7lv3.doc

yankai.doc

import os

import fnmatch

path_test = r"F:python_test1Yankerp_test

esult_test"

for f in os.listdir(path_test):

if fnmatch.fnmatch(f, "?.doc"):

print(f)

>>>

0.doc

1.doc

2.doc

3.doc

4.doc

可以结合正则匹配

import os

import fnmatch

path_test = r"F:python_test1Yankerp_test

esult_test"

for f in os.listdir(path_test):

if fnmatch.fnmatch(f, "[1-9].pptx"):

print(f)

>>>

2.pptx

3.pptx

2.2、glob匹配

import os

import fnmatch

import glob

path_test = r"F:python_test1Yankerp_test

esult_test"

for f in glob.glob(f"{path_test}/[0-9].txt"):

print(f)

>>>

F:python_test1Yankerp_test

esult_test.txt

F:python_test1Yankerp_test

esult_test.txt

F:python_test1Yankerp_test

esult_test.txt

F:python_test1Yankerp_test

esult_test.txt

F:python_test1Yankerp_test

esult_test.txt

三、序列化pickle

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import pickle

User_data = {

"Name" : "Yankai",

"Password" : "Pwd123"

}

with open("data.pkl","wb") as f:

pickle.dump(User_data, f)

with open("data.pkl","rb") as f:

data = pickle.load(f)

print(data)

>>>

{'Name': 'Yankai', 'Password': 'Pwd123'}

本页共197段,3722个字符,4757 Byte(字节)