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)


暂无数据
推荐帖子
0条评论
0条评论
3条评论