python文件的使用方法

  1. 文件的类型
    文件是存储在辅助存储器上的数据序列
    文件是数据存储的一种形式
    文件展现形态:文本文件和二进制文件

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

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

在这里插入图片描述
在这里插入图片描述
(图片来自北理工嵩天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
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Cachel wood

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

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

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

打赏作者

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

抵扣说明:

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

余额充值