加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python编程从入门到实践笔记——文件

发布时间:2020-12-17 00:21:53 所属栏目:Python 来源:网络整理
导读:Python编程从入门到实践笔记——文件 file_name = = span style="color: #008000;"# span style="color: #008000;"关键字with在不再需要访问文件后将其关闭。 span style="color: #008000;" span style="color: #008000;"open(path)打开文件 span style="col

Python编程从入门到实践笔记——文件

file_name = =<span style="color: #008000;">#<span style="color: #008000;">关键字with在不再需要访问文件后将其关闭。<span style="color: #008000;">

<span style="color: #008000;">open(path)打开文件<span style="color: #008000;">

<span style="color: #008000;">read()读取整个文件的内容

<span style="color: #008000;">#<span style="color: #008000;">2.文件路径<span style="color: #008000;">

<span style="color: #008000;">Linux和OS X中:with open('text_files/filename.text') as file_object:<span style="color: #008000;">

<span style="color: #008000;">Windows中:with open('text_filesfilename.text') as file_object:

<span style="color: #008000;">#<span style="color: #008000;">3.逐行读取
<span style="color: #000000;">with open(file_name) as file_object:
<span style="color: #0000ff;">for line <span style="color: #0000ff;">in<span style="color: #000000;"> file_object:
<span style="color: #0000ff;">print<span style="color: #000000;">(line.rstrip())

<span style="color: #008000;">#<span style="color: #008000;">4.创建一个包含文件各行内容的列表<span style="color: #008000;">

<span style="color: #008000;">readlines()从文件中读取每一行,并将其存储在一个列表中

<span style="color: #000000;">with open(file_name) as file_object:
lines =<span style="color: #000000;"> file_object.readlines()

<span style="color: #0000ff;">for line <span style="color: #0000ff;">in<span style="color: #000000;"> lines:
<span style="color: #0000ff;">print<span style="color: #000000;">(line.rstrip())

<span style="color: #008000;">#<span style="color: #008000;">5.使用文件的内容<span style="color: #008000;">

<span style="color: #008000;">读取文本文件时候,Python将其中的所有文本都解读为字符串。

<span style="color: #000000;">with open(file_name) as file_object:
lines =<span style="color: #000000;"> file_object.readlines()

pi_string = <span style="color: #800000;">''
<span style="color: #0000ff;">for line <span style="color: #0000ff;">in<span style="color: #000000;"> lines:
pi_string +=<span style="color: #000000;"> line.rstrip()

<span style="color: #0000ff;">print<span style="color: #000000;">(pi_string)

<span style="color: #008000;">#<span style="color: #008000;">6.包含一百万位的大型文件<span style="color: #008000;">

<span style="color: #008000;">复习一下圆周率:3.14159265358979323846264338327950288419716939937510...

<span style="color: #008000;">#<span style="color: #008000;">7.圆周率中包含你的生日吗
birthday = input(<span style="color: #800000;">"<span style="color: #800000;">Enter your birthday,in the form mmddyy: <span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">if birthday <span style="color: #0000ff;">in<span style="color: #000000;"> pi_string:
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Your birthday appears in the first million digits of pi!<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Your birthday does not appear in the first million digits of pi.<span style="color: #800000;">"<span style="color: #000000;">)

<span style="color: #008000;">#<span style="color: #008000;">10.2写入文件<span style="color: #008000;">

<span style="color: #008000;">1.写入空文件<span style="color: #008000;">

<span style="color: #008000;">open()第一个实参是要打开的文件名称;第二个实参是要以写入模式打开这个文件。<span style="color: #008000;">

<span style="color: #008000;">读取模式(’r‘)(默认)、写入模式(’w‘)、附加模式(’a‘)、读写模式(’r+‘)<span style="color: #008000;">

<span style="color: #008000;">Python只能将字符串写入文本文档。要存储数值,可使用str()以后写入

file_name = <span style="color: #800000;">'<span style="color: #800000;">programming.txt<span style="color: #800000;">'<span style="color: #000000;">

with open(file_name,<span style="color: #800000;">'<span style="color: #800000;">w<span style="color: #800000;">'<span style="color: #000000;">) as file_object:
file_object.write(<span style="color: #800000;">"<span style="color: #800000;">I love programming.<span style="color: #800000;">"<span style="color: #000000;">)

<span style="color: #008000;">#<span style="color: #008000;">2.写入多行<span style="color: #008000;">

<span style="color: #008000;">在write()语句中加入换行符’n‘

<span style="color: #008000;">#<span style="color: #008000;">3.附加到文件<span style="color: #008000;">

<span style="color: #008000;">附加模式’a‘

with open(file_name,<span style="color: #800000;">'<span style="color: #800000;">a<span style="color: #800000;">'<span style="color: #000000;">) as file_object:
file_object.write(<span style="color: #800000;">"<span style="color: #800000;">I love programming.n<span style="color: #800000;">"<span style="color: #000000;">)
file_object.write(<span style="color: #800000;">"<span style="color: #800000;">I love playing basketball.n<span style="color: #800000;">")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读