简析python 文件操作

一、打开并读文件

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可以对比来记。

相关文章

如何用python打开文件

针对上一篇文章中,python打印内存并保存的相关操作中使用了python创建文件,打开文件,写入文件等一系列的操作;今天我们来讲解一下如何用python打开文件。一、文件处理在 Python 中使用...

Python怎么运行py文件,编程入门指南

1.摘要本文使用以下四种方法运行py文件:①使用命令行运行py文件,具体为先切换到对应路径,再输入“python 文件名.py ”;②以PowerShell方式打开路径运行py文件,首先进入到py文件...

Python 中的文件操作详解

文件处理操作作为日常编程中的一个必要操作之一,无论是读取日志文件、配置文件、还是将爬虫数据输出成其他的文件,都是文件操作中不可或缺的一部分。Python作为目前比较流行的编程语言,也提供了强大的文件操...

一文掌握Python中的文件操作

Python为文件处理提供了一组通用的工具和函数,使得对文件执行各种操作相对简单。这些操作包括打开文件、阅读其内容、写入新数据、追加到现有文件等。文件可以包含广泛的信息,从纯文本文档到图像、数据库、电...

4个步骤教你学会用Pycharm如何运行.py文件,简单上手(建议收藏)

这篇文章主要介绍了Pycharm如何运行.py文件的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧!Pycharm时一个...

5分钟掌握在Python中处理文件的8种基本操作

I在 Python 中处理文件是一项常见任务,Python 提供了几个内置函数和模块来帮助您读取、写入和操作文件。以下是在 Python 中处理文件时可以执行的一些基本操作:打开文件:您可以使用该函数...