欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

荐 python文件的使用方法

程序员文章站 2022-07-07 19:59:24
python文件的使用方法,包括读入文件,写入文件...
  1. 文件的类型
    文件是存储在辅助存储器上的数据序列
    文件是数据存储的一种形式
    文件展现形态:文本文件和二进制文件

本质上所有文件都用二进制形式形式存储。文本文件是由单一特定编码组成的文件,二进制文件直接由0和1组成,没有特定的编码。

  1. 文件的打开和关闭
    <变量名> = open(<文件名>, <打开模式>)
    源文件和程序在同一目录下可以省略路径(即使用相对路径),否则需要使用绝对路径。
file=open('meimei.txt','r')   #相对路径,只读
file=open('C:Users/lenovo/coding/meimei.txt','r')   #绝对路径,只读

荐
                                                        python文件的使用方法
荐
                                                        python文件的使用方法
(图片来自北理工嵩天python慕课)
文件的关闭

file=open('F:meimei.txt','r')
print(file.read())
file.close()   #文件的关闭

文件内容的读取
文件内容的读取主要有三种方式,分别是read,readline,和readlines函数
三种方式各有特点

操作方法 描述
file.read(size=-1) 读入全部内容,如果给出参数,读入前size长度
file.readline(size=-1) 读入一行内容,如果给出参数,读入该行前size长度
file.readlines(hint=-1) 读入文件所有行,以每行为元素形成列表如果给出参数,读入前hint行
  • 文件的全文本读取
    (一次读取全部文本内容)
file=open('F:meimei.txt','r')
print(file.read())
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a dazzling haze
A mysterious way about you dear
Have I known you 20 seconds or 20 years
Can I go where you go
Can we always be this close
Forever and ever
And ah take me out
And take me home
You're my my my my
Lover

(读取全部文本内容并形成列表)

file=open('F:meimei.txt','r')
print(file.readlines())
file.close()

["We can leave the Christmas lights up 'til January\n", 'This is our place we make the rules\n', "And there's a dazzling haze\n", 'A mysterious way about you dear\n', 'Have I known you 20 seconds or 20 years\n', 'Can I go where you go\n', 'Can we always be this close\n', 'Forever and ever\n', 'And ah take me out\n', 'And take me home\n', "You're my my my my\n", 'Lover']
  • 文件的逐行读取

(readline方法)

file=open('F:meimei.txt','r')
txt=file.readline()    #分行读入,逐行处理
for txt in file:
    print(txt)
file.close()

This is our place we make the rules

And there's a dazzling haze

A mysterious way about you dear

Have I known you 20 seconds or 20 years

Can I go where you go

Can we always be this close

Forever and ever

And ah take me out

And take me home

You're my my my my

Lover

(read方法)


file=open('F:meimei.txt','r')
txt=file.readlines()   #一次读入,分行处理
for line in txt:
    print(line)
file.close()

read和readlines方法都是一次性读入整个文件,如果文件的规模特别大,可能会直接破坏软件,所以一般的小文件可以直接读取,较大的文件使用readline逐行读取,或者可以采取切片的方式。

file=open('F:meimei.txt','r')
print(file.read(100))
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a 

文件的读取还可以使用with open 方式,示例如下

path='F:meimei.txt'
with open(path,'r') as file:
    txt=file.read()
print(txt)
file.close()
  • 数据的文件写入
path='F:meimei.txt'
file=open(path,'a')  #追加写模式
txt=file.write('I love meimei\n')
file.close()

file=open(path,'r') 
print(file.read())
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a dazzling haze
A mysterious way about you dear
Have I known you 20 seconds or 20 years
Can I go where you go
Can we always be this close
Forever and ever
And ah take me out
And take me home
You're my my my my
Lover
I love meimei
path='F:meimei.txt'
file=open(path,'a')    #追加写模式
lis=['meimei\n','taylor\n','swift\n']
file.writelines(lis)
file.close()

file=open(path,'r') 
print(file.read())
file.close()

We can leave the Christmas lights up 'til January
This is our place we make the rules
And there's a dazzling haze
A mysterious way about you dear
Have I known you 20 seconds or 20 years
Can I go where you go
Can we always be this close
Forever and ever
And ah take me out
And take me home
You're my my my my
Lover

meimei
taylor
swift

本文地址:https://blog.csdn.net/weixin_46530492/article/details/107158497