(Python Beginner) Output file is empty in my code

I am relatively new to Python and working with inputting and outputting in files. Here is the input file:

1 3
1 1
1 0
20 30

and here is my code that takes this as "soccer_in.txt" and is suppose to output the following into "soccer_out.txt":

Season: 1, Games Played: 1, Points earned: 3
Possible Win-Tie-Loss Records
-----------------------------
1-0-0

Season: 2, Games Played: 1, Points earned: 1
Possible Win-Tie-Loss Records
-----------------------------
0-1-0

Season: 3, Games Played: 1, Points earned: 0
Possible Win-Tie-Loss Records
-----------------------------
0-0-1

Season: 4, Games Played: 20, Points earned: 30
Possible Win-Tie-Loss Records
-----------------------------
10-0-10
9-3-8
8-6-6
7-9-4
6-12-2
5-15-0

using this code:

def process_season(output_file, season, games_played, points_earned):
    output_file.write("Season: " + str(season) + ", Games Played: " + str(games_played) +
          ", Points earned: " + str(points_earned))
    output_file.write("Possible Win-Tie-Loss Records")
    output_file.write("-----------------------------")
    wins = int(points_earned) // 3
    ties = int(points_earned) % 3
    losses = int(games_played) - wins - ties
    while (wins >= 0) and (losses >= 0):
            output_file.write(str(wins) + "-" + str(ties) + "-" + str(losses))
            wins -= 1
            ties += 3
            losses -= 2
# --------------------------------------
def process_seasons(input_file, output_file):
    season_number = 0
    for season in input_file:
        season_number += 1
    process_season(output_file, season_number, season[0], season[1])
# --------------------------------------
f_in=open("soccer-in.txt", "r")
f_out=open("soccer-out.txt", "w+")
process_seasons(f_in, f_out)

I am not getting any errors, but my output file is empty when I run my code. I am not sure what is happening and any help would be appreciated. Thank you!

Edit: None of the proposed solutions have worked so far. I run the file, and "soccer-output.txt" is still blank. I see the issue with closing the file, but that has not solved the fact that the output file is empty.

EDIT 2: NEVERMIND! i had the input file open on my computer which was not allowing the code to work. Thank you all

You should rewrite your code into context manager form:

def process_season(output_file, season, games_played, points_earned):
    output_file.write("Season: " + str(season) + ", Games Played: " + str(games_played) +
          ", Points earned: " + str(points_earned))
    output_file.write("Possible Win-Tie-Loss Records")
    output_file.write("-----------------------------")
    wins = int(points_earned) // 3
    ties = int(points_earned) % 3
    losses = int(games_played) - wins - ties
    while (wins >= 0) and (losses >= 0):
            output_file.write(str(wins) + "-" + str(ties) + "-" + str(losses))
            wins -= 1
            ties += 3
            losses -= 2
# --------------------------------------
def process_seasons(input_file, output_file):
    season_number = 0
    for season in input_file:
        season_number += 1
    process_season(output_file, season_number, season[0], season[1])
# --------------------------------------
with open("soccer-in.txt", "r") as f_in:
    with open("soccer-out.txt", "w+") as f_out:
        process_seasons(f_in, f_out)

With context manager, file object will be automatically closed. So you needn't to worry about the closing.

The problem is that you are failing to close the file after you have opened it. To help in preventing this, please use the with context manager to read and write files.

with open(file_name, 'r') as f:
    f.read()
with open(file_name, 'w') as f:
    f.write(data)

You do not NEED to close your files. Although you SHOULD.

Garbage Collector will take care of it. but you do need to change your code, accounting for newlines with \n and also you need to split your lines into words in your process_seasons function. ..The following code runs on my computer and supplies the output you are looking for.

def process_season(output_file, season, games_played, points_earned):
    output_file.write("Season: " + str(season) + ", Games Played: " + str(games_played) +
          ", Points earned: " + str(points_earned) + '\n')
    output_file.write("Possible Win-Tie-Loss Records\n")
    output_file.write("-----------------------------\n")
    wins = int(points_earned) // 3
    ties = int(points_earned) % 3
    losses = int(games_played) - wins - ties
    while (wins >= 0) and (losses >= 0):
            output_file.write(str(wins) + "-" + str(ties) + "-" + str(losses)+'\n')
            wins -= 1
            ties += 3
            losses -= 2
# --------------------------------------
def process_seasons(input_file, output_file):
    season_number = 0
    for season in input_file:
        season_number += 1
        seas = season.split()
        process_season(output_file, season_number, seas[0], seas[1])
# --------------------------------------
f_in=open("soccer-in.txt", "r")
f_out=open("soccer-out.txt", "w+")
process_seasons(f_in, f_out)

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image