2017-10-09 38 views
2

我对Python比较陌生,正在处理输入和输出文件。这里是输入文件:(Python初学者)我的代码中输出文件为空

1 3 
1 1 
1 0 
20 30 

,这里是我的代码,将其作为“soccer_in.txt”,并假设输出以下为“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 

代码:

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) 

我没有得到任何错误,但我的输出文件是空的,当我运行我的代码。我不确定发生了什么事情,任何帮助将不胜感激。 谢谢!

编辑:到目前为止,所提出的解决方案都没有工作。我运行该文件,“soccer-output.txt”仍然是空白。我看到关闭文件的问题,但这并没有解决输出文件为空的事实。

编辑2:NEVERMIND!我在我的电脑上打开了输入文件,该文件不允许代码工作。谢谢大家

+1

把'output_file.close()'放在'process_season()'函数的末尾,看它是否有效。 – Unni

+0

你在哪里关闭输出文件? – toonarmycaptain

+0

你不需要关闭文件。 垃圾回收期间,Python将为您关闭文件。 这不是错误的原因。 – 0TTT0

回答

0

你应该重写你的代码上下文管理形式:

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) 

随着上下文管理器,文件对象将自动关闭。所以你不必担心关闭。

0

问题在于打开文件后无法关闭文件。为了帮助防止这种情况,请使用with context manager来读取和写入文件。

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

您不需要关闭文件。虽然你应该。

垃圾收集器会照顾它。 但您确实需要更改您的代码,并使用\n来说明换行符,并且您还需要将您的行分割为process_seasons函数中的单词。 ..以下代码在我的电脑上运行,并提供您正在查找的输出。

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) 
+1

不要鼓励这种可怕的做法。这在很多情况下会咬人。虽然我不会失望,因为你确实暴露了一个bug。 – Shadow