Python高级语法之文件基础

文件基础

主流编码:ASCII GB2312GBK Unicode UTF-8
常用模块:
  • os 提供了大量的操作文件、目录的方法
  • pathlib2 文件系统路径操作
  • openpyxl 操作Excel文件
  • python-docx 操作Word文件
  • python-pptx 操作PowerPoint文件
  • opencv-python 图像处理模块,提供了强大的图像处理相关功能
打开文件

打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用打开文件后,再操作

open(file mode='r')
  • 解析
    • file:必需,文件路径(相对路径或者绝对路径)
    • mode:可选,文件打开模式
      • r:读文件(read)
      • w:写文件(write)
      • a:在文件尾部追加内容(append)
      • b:二进制文件(图像文件,binary)
      • t:文本文件(text)
      • +:复合(r+:读写、写时从头开始覆盖;w+:读写、清空源文件后读写)
关闭文件

使用open()方法一定要保证关闭文件对象,即调用close()方法

file.close()

使用with…as…,隐式调用close()方法关闭文件

with open('file.txt','r') a
文件属性
  • file.name:返回文件的名称
  • file.mode:返回打开文件时,采用的文件打开模式
  • file.encoding:返回打开文件是使用的编码格式
  • file.closed:判断文件是否已经关闭
with open("file.txt",'r') as f:
    print(f.name)
    print(f.mode)
    print(f.encoding)
    print(f.closed)
print(f.closed)
#结果
#file.txt
#r
#cp936 即为GBK
#False
#True

读取文件

函数read()

逐个字节或者字符读取文件中的内容,有参数时,读取参数个字节或字符

with open("file.txt",'r') as f:
	msg = f.read(6)
print(msg)
#结果
#123
#12
函数readline()

逐行读取文件中的内容

with open("file.txt",'r') as f:
	msg = f.readline()
	msg1 = f.readline(2)
print(msg,end="")
print(msg1,end="")
#结果
#123
#12
函数readlines()

一次读取文件中的多行内容

with open("file.txt",'r') as f:
	msg = f.readlines()
print(msg)
#结果
#['123\n', '123\n', '123\n', '455\n', '456']

示例:将file.txt文件中的内容以指定格式显示

'''
原格式:
2021/6/2,1533
2021/6/3,1515
2021/6/4,2332
目标格式:
时间是: 2021-06-02 销售额是:1533.0
时间是: 2021-06-03 销售额是:1515.0
时间是: 2021-06-04 销售额是:2332.0
'''

from datetime import  datetime
file = "file.txt"
with open(file,"r") as f:
	for line in f.readlines():
		myData,price = line.split(",")
		#print(myData)
		#print(price)
		dt = datetime.strptime(myData,"%Y/%m/%d")
		#print(dt)
		price = float(price)
		#print(dt)
		#print(price)
		print("时间是:",str(dt.date()),"销售额是:"+str(price))
#结果
#时间是: 2021-06-02 销售额是:1533.0
#时间是: 2021-06-03 销售额是:1515.0
#时间是: 2021-06-04 销售额是:2332.0

存储文件

函数write()

写入一个字符或者一个字符串

'''
file.txt原有内容:88888888
'''

#示例1
file = "file.txt"
with open(file,"r+") as f:
	f.write("666666")
print(f.closed)
#结果
#True
#file.txt现有内容:66666688

#示例2
file = "file.txt"
with open(file,"w+") as f:
	f.write("666666")
print(f.closed)
#结果
#True
#file.txt现有内容:666666
函数writelines()

用于向文件中写入一序列的字符串或字节串,序列可以是列表、元组、字典、集合等

'''
file.txt原有内容:
111
222
333
'''
x = (open("file.txt","r"))
y = (open("file1.txt","w"))
y.writelines(x.readlines())
x.close()
y.close()
#结果
'''
file1.txt现有内容:
111
222
333
'''
datetime模块
from datetime import datetime
dt = datetime.strptime("2022/10/05","%Y/%m/%d")
print(dt)
print(dt.date())
print("time:"+str(dt.date()))
#结果
#2022-10-05 00:00:00
#2022-10-05
#time:2022-10-05
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lilinwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值