open()
打开文件,一次传入一行数据,能够结合for循环和readline()来使用python
close()
用来关闭open打开的文件less
the_file = open('sketch.txt') the_file.close()
例子:ide
>>> data = open('/root/python_test/site_list.txt') >>> print(data.readline()) www.godblessyuan.com
一些基础的目录管理函数函数
>>> import os >>> os.getcwd() #获取当前目录 '/root' >>> os.chdir('/root/python_test') #切换目录 >>> os.getcwd() '/root/python_test' >>>
>>> data = open('/root/python_test/headfirstpython/sketch.txt') >>> for each_line in data: ... (role,line_spoken) = each_line.split(':') #这里使用idel时候,须要注意的是代码之间的缩进 ... print role ... print line_spoken ... Other Man Anyway, I did. Man You most certainly did not! Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: too many values to unpack
遇到报错了,报错意思大概是太多值致使没有被处理,检查发现是由于有些数据是超过一个冒号的,因此这些数据会出错,由于split()处理不了,可是检查了split函数的使用说明,发现是能够支持这种状况的,ui
>>> help(each_line.split) Help on built-in function split: split(...) S.split([sep [,maxsplit]]) -> list of strings Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. (END)
1.检查方式能够参考上面的方法。this
2.支持这种状况的参数是一个叫maxsplit的参数,若是有maxsplit的话,那么至可能是maxsplit的数量以上的分界符才会被处理,这样就很好的避免太多分界符的状况了。spa
如这样:code
data = open('sketch.txt') for each_line in data: (role, line_spoken) = each_line.split(':', 1) print role print line_spoken data.close()
不过,即便加了参数,仍是遇到报错了ci
Man Oh no you didn't! Other Man Oh yes I did! Man Oh look, this isn't an argument! Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: need more than 1 value to unp
这里是说须要超过一个值去处理,查看了数据,发现是有些数据没有冒号致使程序处理失败。rem
首先经过观察find()方法对于不一样的数据返回的值是不一样的。
>>> each_line = 'iiiii' >>> each_line.find(':') -1 >>> each_line = 'iiiii:' >>> each_line.find(':') 5 >>>
而后可使用的逻辑有2种,一种是if判断,另一种是try:expoet
try: 你的代码(可能会致使运行错误的) except: 错误回复代码
这种方式的机制是经过捕获某代码的错误,而后执行响应的修复代码,例子:
data = open('sketch.txt') for each_line in data: try: (role, line_spoken) = each_line.split(':', 1) print role print line_spoken except: pass data.close()
若是
(role, line_spoken) = each_line.split(':', 1) print role print line_spoken
这里有其中一句代码是执行失败的,都会转到pass里面去,pass表明空语句,或者null,什么也不作。
或者就是使用最简单的if判断
for each_line in data: if not each_line.find(':') == -1: #not关键字是将一个条件取反 (role, line_spoken) = each_line.split(':', 1) print(role, end='') print(' said: ', end='') print(line_spoken, end='') data.close()
不过须要注意的是,像下面这种多重try:except的代码是很容易影响到咱们判断那一部分代码才是真正有问题的代码,由于不管里面和外面的try出错了,都会返回 print('The datafile is missing!'),这样就不能判断是那部分代码有问题了。
try: data = open('sketch.txt') for each_line in data: try: (role, line_spoken) = each_line.split(':', 1) print role print line_spoken except: pass data.close() except: print('The datafile is missing!')
因此须要加一些标记,标识(ValueError-数据不符合指望的格式时会出现,IOError-数据没法正常访问时会出现。)
try: data = open('sketch.txt') for each_line in data: try: (role, line_spoken) = each_line.split(':') print role print line_spoken except ValueError: pass data.close() except IOError: print('The datafile is missing!')
http://www.godblessyuan.com/2015/04/20/head_first_python_chapter_3_lea...