ladysosoli
ladysosoli
2018-04-16 13:33
采纳率: 95.7%
浏览 1.6k

Python2.7 使用多个文件处理FileNotFoundError异常

#!/usr/bin/env python

-*- coding: utf-8 -*-

def count_words(filename):
"""计算一个文件大致包含多少个单词"""
try:
with open(filename) as file_object:
contents=file_object.read()
except FileNotFoundError:
message="Sorry,the file "+filename+" does not exist."
print(message)
else:
#计算文件大致包含多少个单词
words=contents.split()
num_words=len(words)
print("The file "+filename+" has about "+str(num_words)+" words.")
filenames=['alice.txt','siddhartha.txt','moby_dick.txt','little_women.txt']
for filename in filenames:
count_words(filename)


图片说明
其中siddhartha.txt不在文件夹内,想请问一下这是什么情况~

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • qdf8474
    qdf_csdn 2018-04-17 01:08
    已采纳

    函数也需要缩进,按下面这段试下:

    #!/usr/bin/env python
    -*- coding: utf-8 -*-
    def count_words(filename):
    """计算一个文件大致包含多少个单词"""
        try:
                with open(filename) as file_object:
                        contents=file_object.read()
                except FileNotFoundError:
                        message="Sorry,the file "+filename+" does not exist."
                print(message)
        else:
                #计算文件大致包含多少个单词
                words=contents.split()
                num_words=len(words)
                print("The file "+filename+" has about "+str(num_words)+" words.")
                filenames=['alice.txt','siddhartha.txt','moby_dick.txt','little_women.txt']
                for filename in filenames:
                        count_words(filename)
    
    点赞 评论
  • QQ136771056
    zhouyirong09 2018-04-16 14:38

    哥们,python要求严格的冒号过行后要留一个tab缩进,另外不知道你下面的else哪来的,给的注释掉了,以后这种error多查google
    def count_words(filename):
    """计算一个文件大致包含多少个单词"""
    try:
    with open(filename) as file_object:
    contents=file_object.read()
    except FileNotFoundError:
    message="Sorry,the file "+filename+" does not exist."
    print(message)
    #else:
    #计算文件大致包含多少个单词
    words=contents.split()
    num_words=len(words)
    print("The file "+filename+" has about "+str(num_words)+" words.")
    filenames=['alice.txt','siddhartha.txt','moby_dick.txt','little_women.txt']
    for filename in filenames:
    count_words(filename)

    点赞 评论
  • zlj_zlj
    小小鱼儿大大海 2018-04-17 00:47

    try后面的语句都没有进行缩进呢

    点赞 评论
  • qdf8474
    qdf_csdn 2018-04-17 01:09

    如楼上所说,Python是用缩进控制代码的,你按下面这段试下?

    #!/usr/bin/env python
    -*- coding: utf-8 -*-
    def count_words(filename):
    """计算一个文件大致包含多少个单词"""
    try:
        with open(filename) as file_object:
            contents=file_object.read()
        except FileNotFoundError:
            message="Sorry,the file "+filename+" does not exist."
        print(message)
    else:
        #计算文件大致包含多少个单词
        words=contents.split()
        num_words=len(words)
        print("The file "+filename+" has about "+str(num_words)+" words.")
        filenames=['alice.txt','siddhartha.txt','moby_dick.txt','little_women.txt']
        for filename in filenames:
            count_words(filename)
    
    点赞 评论
  • qq_38175744
    EE_Coder 2018-04-17 05:08

    另一种获取异常信息的途径是通过sys模块中的exc_info()函数。该函数回返回一个三元组:(异常类,异常类的实例,跟中记录对象)

    点赞 评论
  • qq_35721136
    Mocdasan 2018-04-17 08:49

    可能是您的缩进有问题,在现在看来您的缩进是有问题的。你注意缩进规则。

    点赞 评论

相关推荐