一、从文件中读取数据

#!/usr/bin/env python

with open('pi') as file_object:
contents = file_object.read()
print(contents) ===================================
3.1415926
5212533
2324255

1、逐行读取

#!/usr/bin/env python

filename = 'pi'

with open(filename) as file_object:
for line in file_object:
print(line) ===================================
3.1415926 5212533 2324255
#!/usr/bin/env python

filename = 'pi'

with open(filename) as file_object:
for line in file_object:
print(line.rstrip()) ==================
3.1415926
5212533
2324255

2、创建一个包含文件各行内容的列表

#!/usr/bin/env python

filename = 'pi'

with open(filename) as file_object:
lines = file_object.readlines() #readlines()方法是从文件中读取每一行,并将其存储在一个列表中 for line in lines:
print(line.rstrip()) ==============================
3.1415926
5212533
2324255

3、使用文件内容

#!/usr/bin/env python

filename = 'pi'

with open(filename) as file_object:
lines = file_object.readlines() pi_string = ''
for line in lines:
pi_string += line.strip() print(pi_string)
print(len(pi_string)) ========================================
3.141592652125332324255
23

二、写入文件

1、写入空文件

#!/usr/bin/env python

filename = 'programming.txt'

with open(filename,'w') as file_object:
file_object.write("I love programming!")

2、写入多行

#!/usr/bin/env python

filename = 'programming.txt'

with open(filename,'w') as file_object:
file_object.write("I love programming!\n")
file_object.write("yes!\n")

3、附加到文件

#!/usr/bin/env python

filename = 'pi'

with open(filename,'a') as file_object:
file_object.write("I love programming!\n")
file_object.write("yes!\n")

三、异常

1、使用try-except代码块

#!/usr/bin/env python

try:
print(5/0)
except ZeroDivisionError:
print("You cant divide by zero!")

Python 文件和异常的更多相关文章

  1. Python入门学习系列——Python文件和异常

    从文件中读取数据 首先准备一个文本文件,文件中存储着普通文本数据.读取文件需要调用open()和read()函数. 读取整个文件 代码示例: with open('pi_digits.txt') as ...

  2. Python文件和异常

    程序和运行时数据是在内存中驻留的,涉及到数据交换的地方,通常是磁盘.网络等,因此需要IO接口. IO编程中,Stream(流)是一个很重要的概念,可以把流想象成一个水管,数据就是水管里的水,但是只能单 ...

  3. python 文件与异常

    ####文件### r: -只能读 不能写 -读取文件不存在,是会报错 r+: - 可以执行读写操作; - 文件不存在, 报错: w: -只能写,不能读 -会清空文件内容 -文件不存在,会新建文件 w ...

  4. Python的文件及异常

    1. Python的文件及异常 1.1 文件操作 1.1.1 从文件中读取数据 许多情况下,我们的信息是存储在文本中的.例如对用户行为的分析,用户访问系统或者网站的访问信息会被存储于文本中,然后对文本 ...

  5. python入门学习:9.文件和异常

    python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件  首先创建一个pi_ ...

  6. [Python 从入门到放弃] 6. 文件与异常(二)

    本章所用test.txt文件可以在( [Python 从入门到放弃] 6. 文件与异常(一))找到并自行创建 现在有个需求,对test.txt中的文本内容进行修改: (1)将期间的‘:’改为‘ sai ...

  7. Python文件操作,异常语法

    1.文件 2.异常 1.文件的输入输出 #1.打开文件 open 函数open(file,[option])#file 是要打开的文件#option是可选择的参数,常见有 mode 等​#2.文件的打 ...

  8. Python获得文件时间戳 异常访问监控 邮件定时提醒

    Python获得文件时间戳  异常访问监控 邮件定时提醒

  9. Python之文件和异常IO

    文件和异常 读写文本文件 读取文本文件时,需要在使用open函数时指定好带路径的文件名(可以使用相对路径或绝对路径)并将文件模式设置为'r'(如果不指定,默认值也是'r'),然后通过encoding参 ...

随机推荐

  1. Celery-4.1 用户指南: Concurrency (并发)

    简介 Eventlet 的主页对它进行了描述:它是一个python的并发网络库,可以让你更改如何运行你的代码而不是怎么编写代码. 对高可扩展非阻塞IO操作,它使用 epoll或者libevent. C ...

  2. Codeforces Round #310 (Div. 2)556ABCDE

    https://github.com/Anoxxx/OI/blob/master/Anoxx/Contest10 github自取

  3. 程序中使用log4J打印信息的两种方式

    (1)通过org.apache.commons.logging.Log 接口实例化: public static Log log = LogFactory.getLog(String name); p ...

  4. scrapy(1)安装

    用的是python3.6 pip install -i https://pypi.douban.com/simple/ scrapy scrapy startproject Article scrap ...

  5. JS中substring()方法(用于提取字符串中介于两个指定下标之间的字符)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. hibernate学习笔记(1)基础配置与jar包

    下载hibernate基础jar包,并解压hibernate-core-4.2.4.final 在myeclipse中添加hibernate的dtd支持: location为D:\学习\imooc-h ...

  7. 下拉框value ,selectedIndex

  8. Apache的DBUtils框架学习(转)

    原文地址:http://www.cnblogs.com/xdp-gacl/p/4007225.html 一.commons-dbutils简介 commons-dbutils 是 Apache 组织提 ...

  9. UIScrollView 实现比例缩放

    #import "RootViewController.h" @interface RootViewController ()<UIScrollViewDelegate> ...

  10. linux所有文件中查找关键字的命令

     grep 192.168.1.1 * -r    在所有文件中查找192.168.1.1