python-文件的操作

python-文件的操作

一、文件操作的流程 

python文件的操作流程如下:

         

 

例子:

现有如下一个文件my-hear

Every night in my dreams

在梦中的每个夜晚

I see you,I feel you

我看见你,我感觉到你

That is how I know you go on

我就是这样知道你的

Far across the distance

远处

And spaces between us

以及我们之间的空间

You have come to show you go on

你是来带你去的

Near far

近距离

Wherever you are

无论你在哪里

I believe

我相信

That the heart does go on

心脏确实在继续

Once more you open the door

你又一次开门

And you're here in my heart

你在我心中

And my heart will go on and on

我的心会不停地跳动

Love can touch us one time

爱可以触摸我们一次

And last for a lifetime

而且会持续一辈子

And never let go till we're gone

在我们走之前永远不要放手

Love was when I loved you

爱是我爱你的时候

One true time I hold to

一次真正的机会

In my life well always go on

在我的生活中,好的总是继续

Near far

近距离

Wherever you are

无论你在哪里

I believe

我相信

That the heart does go on

心脏确实在继续

Once more you open the door

你又一次开门

And you're here in my heart

你在我心中

And my heart will go on and on

我的心会不停地跳动

you're here

你在这儿

There's nothing I fear

我什么都不怕

And I know

我知道

That my heart will go on

我的心会继续

We'll stay forever this way

我们会一直这样

You are safe in my heart

你在我心里很安全

And my heart will go on and on

我的心会不停地跳动
View Code

文件的基本操作如下:

file = open("my-heart")  #打开文件
one_line=file.readline()   #把文件中的光标所在行,赋值到one_line
print("one line:",one_line)#打印
print("剩下的内容".center(30,"="))#分割符
data = file.read() #把剩下文件内容赋值到data中
print(data) #打印

文件打开模式有:r、w 、a

1:r代表,以只读方式打开文件,这个方式是默认的。

2:w代表,以只写的方式打开文件。【这种方式不可读,如果不存在,就创建,存在则删除,重写】

3:a代表,以追加方式打开文件【可以读文件,不存在则建仓;存在就是后面追加内容】

“+”表示可以同读写文件

1、r+【可读写文件,同时可追加】

2、w+【可写,可读】

3、a+【可读,可追加】

"U"表示在读取数据时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

1、rU

2、r+U

"b"表示处理二进制文件(windowsth 处理二进制时需要进行标注,linux不需要)

1、rb

2、wb

3、ab

使用with语句打开文件可以避免打开文件忘记关闭

语法 with  open ("文件",'r')  as file1,  ("文件",'r')  as file2

psss

猜你喜欢

转载自www.cnblogs.com/kezi/p/11939203.html