【Python学习/教程3】文件与异常

Python中的基本输入机制是基于行的:从文本文件想程序读入数据时,一次会达到一个数据行。

Python的open() BIF就是用来与文本交互。使用open() BIF处理文件中的数据时,会创建一个迭代器从文件向你得代码输入数据行,一次传入一行数据。

#打开文件
the_file=open('sketch.txt')
#对数据做一些处理
#关闭文件
the_file.close()

接下来我们自己试验下(这后面是python代码和注释)

>>> import os
>>> os.getcwd()
'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36-32'
>>> os.chdir('C:\Users\Administrator\Desktop\filePython/three')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>> os.chdir('C:/Users/Administrator/Desktop/filePython/three')
>>> os.getcwd()
'C:\\Users\\Administrator\\Desktop\\filePython\\three'
>>> data=open('sketch.txt')
>>> print(data.readline(),end='')
We really think there's a lot of benefit to be had from actually typing in the code and getting it to run. If you really don't want to type in the code yourself, check out the book's support page at Head First Labs, where the code can be downloaded on a chapter-by-chapter, page-by-page basis.
>>> print(data.readline(),end='')

>>> print(data.readline(),end='')
Resources
>>> print(data.readline(),end='')

>>> print(data.readline(),end='')
That said, we do have the stuff referred to in the book - the data files, folder structures, modules, CSS, and so on. Simply right-click on these links to save any resource to your computer:
>>> data.seek(0)
0
>>> data.seek(0)
0
>>> print(data.readline(),end='')
We really think there's a lot of benefit to be had from actually typing in the code and getting it to run. If you really don't want to type in the code yourself, check out the book's support page at Head First Labs, where the code can be downloaded on a chapter-by-chapter, page-by-page basis.
>>> data.seek(0)
0
>>> for line in data:
	print(line,end='')

	
We really think there's a lot of benefit to be had from actually typing in the code and getting it to run. If you really don't want to type in the code yourself, check out the book's support page at Head First Labs, where the code can be downloaded on a chapter-by-chapter, page-by-page basis.

Resources

That said, we do have the stuff referred to in the book - the data files, folder structures, modules, CSS, and so on. Simply right-click on these links to save any resource to your computer:

Chapter 3 & 4: Here is the sketch.txt data file.
Chapter 5: Coach Kelly's data: the athlete timing data in TXT files.
Chapter 6: The updated TXT files from Coach Kelly.
Chapter 7: Here is the code for the yate.py module and the template files. You can also download the webapps folder structure (which includes the index.html, CSS and other support files).
Chapter 8: Yet another update from Coach Kelly - his updated athlete data in TXT files. These data files are also used in Chapter 9.
Chapter 10: The GAE-compatible templates. The HTML stylesheets ("borrowed" - without shame - from the good folks who brought the world this excellent introduction to HTML).
Chapter 11: The CSV-formatted pace data file, the find_it.py module and the tm2secs2tm.py module.


#从标准库导入os
import os
#查询当前工作目录
os.getcwd()
#切换工作目录,这里需要注意/不能是\
os.chdir('C:/Users/Administrator/Desktop/filePython/three')
#确认是否切换成功
os.getcwd()
#打开一个命名文件,将文件赋值给一个data的文件类型。
data=open('sketch.txt')
#打印一行
print(data.readline(),end='')
#打印下一行
print(data.readline(),end='')
#打印下下一行
print(data.readline(),end='')
#使用seek()方法返回文件的起始位置
data.seek(0)
#循环遍历data
for line in data:
	print(line,end='')

最后别忘了关闭文件 data.close()

加了异常处理的python代码:

for line in data:
	try:
		(role,line1)=line.split(':',1)
		print(role,end='')
		print(' said: ',end='')
		print(line1,end='')
#except XXX异常,可以捕获特定异常
	except:
		pass
try:
	data=open('sketch.txt')
	for each_line in data:
		try:
			(role,line_spoken)=each_line.split(':',1)
			line_spoken=line_spoken.strip()
			if role=='Man':
				man.append(line_spoken)
			elif role== 'Other Man':
				other.append(line_spoken)
		except ValueError:
			pass
	data.close()
except IOError:
	print('The datafile is missing!')



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值