热线电话:13121318867

登录
2019-01-28 阅读量: 705
如何使用python自有的函数打开文件??

要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符:

f = open('/Users/michael/test.txt', 'r')

标示符'r'表示读,这样,我们就成功地打开了一个文件。

如果文件不存在,open()函数就会抛出一个IOError的错误,并且给出错误码和详细的信息告诉你文件不存在:

>>> f=open('/Users/michael/notfound.txt', 'r')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/michael/notfound.txt'
0.0000
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子