Python Forum
Getting a "Cannot be Opened"" error Message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a "Cannot be Opened"" error Message
#10
def team_average(filename):
    numberOfGames = 0
    soxWins = 0
    try:
        with open(filename, 'r') as file:
            for line in file:
                numberOfGames += 1
                game = line.split()
                scores = game[1]
                scores_list = scores.split('-')
                print('scores_list: {}'.format(scores_list))
                for score in scores_list:
                    if (int(score[0]) - int(score[1])) > 0:
                        soxWins += 1
                    average_win = int(soxWins / numberOfGames) * 100

                    team_average('xxxxxx')
                    print(team_average('red_sox.txt'))
    except:
        print(filename, "Cannot be opened")

team_average('red_sox.txt')
This is why print statements are so useful

results:
Output:
C:\Python35\python.exe M:/python/e-h/f/forum/misc4.py scores_list: ['Red'] red_sox.txt Cannot be opened
  • because score has 'Red' in it, it causes an exception (but not a file exception)
  • The exception is caught, but displays the wrong message (becuase not being qualified)

adding one more print statement makes the problem very clear:
def team_average(filename):
    numberOfGames = 0
    soxWins = 0
    try:
        with open(filename, 'r') as file:
            for line in file:
                numberOfGames += 1
                game = line.split()
                print('game: {}'.format(game))
                scores = game[1]
                scores_list = scores.split('-')
                print('scores_list: {}'.format(scores_list))
                for score in scores_list:
                    if (int(score[0]) - int(score[1])) > 0:
                        soxWins += 1
                    average_win = int(soxWins / numberOfGames) * 100

                    team_average('xxxxxx')
                    print(team_average('red_sox.txt'))
    except:
        print(filename, "Cannot be opened")

if __name__ == '__main__':
    team_average('red_sox.txt')
result:
Output:
game: ['2011-07-02', 'Red', 'Sox', '@', 'Astros', 'Win', '7-5'] scores_list: ['Red'] red_sox.txt Cannot be opened
Reply


Messages In This Thread
RE: Getting a "Cannot be Opened"" error Message - by Larz60+ - Feb-09-2017, 12:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 1,055 Aug-17-2023, 07:53 PM
Last Post: Visiting
  does not save in other path than opened files before icode 3 955 Jun-23-2023, 07:25 PM
Last Post: snippsat
  Another Error message. the_jl_zone 2 1,011 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,320 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question How to get html information from a tab of my default browser opened with webbrowser? noahverner1995 2 4,579 Jan-14-2022, 10:02 AM
Last Post: noahverner1995
  understanding error message krlosbatist 1 1,942 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 45,981 May-29-2021, 10:26 AM
Last Post: snippsat
  Rmarkdown opened by python code - errors Rav013 0 2,125 Apr-27-2021, 03:13 PM
Last Post: Rav013
  f-string error message not understood Skaperen 4 3,390 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 2,393 Feb-18-2021, 07:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020