社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python文件的使用方法

初与久歌_4a05 • 3 年前 • 295 次点击  
  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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/74262
 
295 次点击