百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

简析python 文件操作_python文件内容操作

itomcoil 2025-09-18 20:11 1 浏览

一、打开并读文件

1、file = open('打开文件的路径','打开文件的权限')#打开文件并赋值给file

#默认权限为r及读权限

str = read(num)读文件并放到字符串变量中,其中num表示要读取的字节数,

#默认read函数不加参数是全读

str = file.read()

file.close()#关闭文件,线程回收

2、下面举个列子(程序用华丽的分割线隔开):

首先我们创建一个test.txt文件随便写入下面内容(Apologize的歌词)

Apologize

I'm holding on your rope,

Got me ten feet off the ground

I'm hearin what you say but I just can't make a sound

You tell me that you need me

Then you go and cut me down, but wait

You tell me that you're sorry

Didn't think I'd turn around, and say...

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

file = open(r'/root/python-learn/python1-7/test.txt')

#r,表示防止转义,也可以用\来防止转义

str = file.read()

print(str)

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

输出结果:

3、这里我们要说明下读写指针

#文件读写指针,当读完一次后,str1将接着str后读,但是str后面会自动添加\n

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

file = open(r'/root/python-learn/python1-7/test.txt')

str = file.read(10)

str1 = file.read(10)

print(str)

print(str1)

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

输出结果:

4、为了修改读写指针我们使用到seek()函数

语法:fileObject.seek(offset[,whence])

offset:偏移量

whence:从哪里

0 表示从头开始计算

1 表示从当前稳只计算

2 表示以文件末尾为远点进行计算

需要注意的是,如果文件以a或a+的模式打开,每次进行写操作时,文件操作标记会自动返回到

文件末尾

file.seek(0,0)回到文件开头

file.seek(-1,2)从末尾向前偏移一个,尝试后发现最好用rb的权限读,rb以二进制方式读

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

file = open(r'/root/python-learn/python1-7/test.txt','rb')

file.seek(-20,2)

str1 = file.read(10)

print(str1)

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

输入结果:(能不使用图片就不粘图了提高效率)

[root@vipuser200 python1-7]# python3 file.py

b'around, an'

5、读取行使用函数readline()

str = readline() 读取一行

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

file = open(r'/root/python-learn/python1-7/test.txt')

str1 = file.readline()

print(str1)

str2 = file.readline()

print(str2)

str3 = file.readline()

print(str3)

str4 = file.readline()

print(str4)

str5 = fisle.readline()

print(str5)

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

输出结果:(原文件里面只有一个空行,但程序自带换行符所以会感觉空行比较多)

[root@vipuser200 python1-7]# python3 file1.py

Apologize

I'm holding on your rope,

Got me ten feet off the ground

I'm hearin what you say but I just can't make a sound

6、strlist = readlines() 读取整个文件到字符串列表

字符串列表:['abc','bcd']里面所有元素必须是字符串,可以把文件中的内容

一次性读到字符串列表中。

怎么去掉换行符呢

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

file = open(r'/root/python-learn/python1-7/test.txt')

strlist = file.readlines()

print(strlist)

for var in strlist:#也可以用file.strip()去掉不可见字符

var = var[:-1]#切片首先你得确定你最后一个换行符是单个字符

print(var)

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

输出结果:

[root@vipuser200 python1-7]# python3 file2.py

['Apologize\n', '\n', "I'm holding on your rope,\n", #后面太长不粘了

I'm holding on your rope,

Got me ten feet off the ground

I'm hearin what you say but I just can't make a sound

You tell me that you need me

Then you go and cut me down, but wait

You tell me that you're sorry

Didn't think I'd turn around, and say...

二、文件写操作

1、file.write('str') #在文件中写入字符串,当你使用写模式打开文件的时候,会将

文件里面的内容清空。

首先我们创建一个test1.txt测试文件在里面写入

Aplologize

编写程序

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

file = open(r'/root/python-learn/python1-7/test1.txt','w')

file.write('---------华丽的分割线-------------')

print(file)

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

执行结果:

[root@vipuser200 python1-7]# python3 file3.py

<_io.TextIOWrapper name='/root/python-learn/python1-7/test1.txt' mode='w'

encoding='UTF-8'>

打开test1.txt

---------华丽的分割线-------------

python中的写操作不会默认加换行符(需要自己手动添加)

python中的写不会覆盖原先的内容,只有我们重新打开文件再次使用w模式时候,文件

内容才会覆盖

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

from time import sleep

file = open(r'/root/python-learn/python1-7/test1.txt','w')

file.write('---------华丽的分割线-------------')

file.write('********华丽的星号*********')

file.flush() #强制写入,不需要等到文件关闭

print(file)

sleep(5) #等待5秒

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

输出结果:

[root@vipuser200 python1-7]# python3 file3.py

<_io.TextIOWrapper name='/root/python-learn/python1-7/test1.txt' mode='w'

encoding='UTF-8'>

打开test1.txt文件显示如下(里面使用了sleep()函数)

---------华丽的分割线-------------********华丽的星号*********

2、file.writelines()在文件中写入字符串元组或者是字符串列表

程序如下:

------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------

file = open(r'/root/python-learn/python1-7/test1.txt','w')

strlist = ['aaa','bbb']

file.writelines(strlist)

file.close()

------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------

输出结果:

打开文件test1.txt

aaabbb

如果我们想让输入的字符串换行需要手动加入换行符可以写成如下所示:

strlist = ['aaa\n','bbb\n']

readlines和writelines可以对比来记。

相关推荐

ELK架构部署以及应用_elk部署方案

一、ELK介绍ELK代表的是Elasticsearch,Logstash,KibanaElasticsearch:日志存储、搜索分析功能Logstash:数据收集,日志收集系统Kibana:数据可视化...

本地部署 DeepSeek Janus Pro 文生图大模型

Hello,大家新年好。在这个春节期间最火的显然是DeepSeek了。据不负责统计朋友圈每天给我推送关于DeepSeek的文章超过20篇。打开知乎跟B站也全是DeepSeek相关的内容。...

DotsOCR 环境搭建指南_dot installation

DotsOCR环境搭建指南支持平台:Linux(推荐)或Windows+WSL2项目地址:https://github.com/rednote-hilab/dots.ocr一、Windo...

spark+python环境搭建_pycharm配置spark环境

最近项目需要用到spark大数据相关技术,周末有空spark环境搭起来...目标spark,python运行环境部署在linux服务器个人通过vscode开发通过远程python解释器执行代码准备...

window下sublimeIDE安装python_win10安装python

window下开发python使用sublimeIDE1安装sublimehttp://www.sublimetext.com/22安装PackageControl提供了安装sublime...

JupyterLab 快速环境配置 (一)_jupyter的环境配置

JupyterLab快速环境配置(一)一只小胖子[互联网运营|直播电商|广告行业]从业者软件说明:JupyterLab是一个基于web浏览器的在线文档/代码运行集成环境,支持文档显示/代...

栋察宇宙(二十一):Python 文件操作全解析

分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“Python文件操作全解析”欢迎您的访问!Sharethefun,spreadthe...

外婆都能学会的Python教程(十八):Python读取配置文件绘制图形

前言Python是一个非常容易上手的编程语言,它的语法简单,而且功能强大,非常适合初学者学习,它的语法规则非常简单,只要按照规则写出代码,Python解释器就可以执行。下面是Python的入门教程介绍...

Python自动化办公应用学习笔记38—文件读写方法2

1.文件迭代文件对象是可迭代的,可以逐行迭代文件。withopen('data.txt','r')asfile:forlineinfile:#逐行迭...

简析python 文件操作_python文件内容操作

一、打开并读文件1、file=open('打开文件的路径','打开文件的权限')#打开文件并赋值给file#默认权限为r及读权限str=read(num)读文件并放到字符串变量中,其中num表...

如何在Python中保存文件?如何读取文件?示例代码

Python中保存文件是一项非常基本的任务,它允许我们将程序输出保存到磁盘上,以便以后使用或与他人共享。本文将介绍如何在Python中保存文件的方法,以及如何读取已有的文件和为代码添加注释。使用ope...

高效办公:Python处理excel文件,摆脱无效办公

一、Python处理excel文件1.两个头文件importxlrdimportxlwt其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入。2.读取exce...

python中12个文件处理高效技巧,不允许你还不知道

在Python中高效处理文件是日常开发中的核心技能,尤其是处理大文件或需要高性能的场景。以下是经过实战验证的高效文件处理技巧,涵盖多种常见场景:一、基础高效操作1.始终使用上下文管理器(with语句)...

python 目录结构的规划,应该先建立好

上一篇文章说了【函数、类、模块、包】,现在说一下python一般工程的目录结构一般习惯这样规划目录,在开始一个工程前,最好先把目录结构规划好。一、为什么要有一个比较清晰的目录结构此处省略一万字....

和尧名大叔一起从0开始学Python编程-简单读写文件

0基础自学编程是很痛苦的一件事情,所以我想把自己学习的这个过程记录下来,让想学编程的人少走弯路,大叔文化程度较低,可能会犯一些错误,欢迎大家督促我。今天,我们来学习一下用Python简单读写文件,这里...