首页 > 编程知识 正文

python怎么保存代码,python读取文件并另存为

时间:2023-05-04 07:42:34 阅读:17525 作者:1433

本文总结了python如何保存文件。 需要的伴侣可以试着学习。

读取文件f=open(test.txt )打开文件

读取DATA=f.read(#文件

是打印(数据)

# oneLine=f.readline (

#打印联机#读取第一行

#lines=f.readlines(#将内容逐行读取到一个列表中

#打印行

f.close(#关闭

写文件f=open(output.txt,) w ) ) #output.txt -文件名和格式w - writing

#在此模式下打开文件时,原始文件的内容将被新写入的内容复盖,如果文件不存在,则会自动创建

f.write(I'mgoingtowriteastring ) )。

out=open('output.txt ',' w ' )。

out.write (I ' m fine (nnowisraining!' )

out.close () )

从控制台中输入文件out=open('out.txt ',' w ' ) while True:进行保存

数据=输入(pleaseinputsomething (enterqquit ) : ) ) )。

IF数据!='q':

out.write (数据'n ' ) )。

else:

布雷克

读取从一个文件写入另一个文件的newfile=open('output.txt ) )

data=newFile.read (

out=open('out.txt ',' w ' )。

out.write (数据)

newFile.close (

out.close () )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。