Jump to content

Python help

Flyguygamer
Go to solution Solved by fizzlesticks,

You close the file while still using it.

 

f.close() should be after the loop.

 

Also, you forgot the () on

outToFile.close

I am learning python following online video tutorials but am having issues with this code.

bestStudent = {}f = open('studentgrades.txt', 'r+')for line in f:	name, grade = line.split()	bestStudent[grade] = name		f.close()	bestStudentStr = ""for i in sorted(bestStudent.keys(), reverse=True):			print(bestStudent[i] + 'scored a' + i)			bestStudentStr += bestStudent[i] + 'scored a ' + i + '\n'						print(bestStudentStr)			bestStudentStr = '\nThe Best Students Ranked\n\n' + bestStudentStroutToFile = open('studentrank.txt', mode='w', encoding='utf-8')outToFile.write(bestStudentStr)outToFile.closeprint('Finished Update')

I have the files in the correct directory but when I try to execute the code I get an error on line 3.

Traceback (most recent call last):  File "C:\Users\Jake.JakeHuitt-HP\workspace\learning python\Learning_python.py", line 3, in <module>    for line in f:ValueError: I/O operation on closed file.

What is the issue? Thanks in advance, Jake.

Link to comment
Share on other sites

Link to post
Share on other sites

You close the file while still using it.

 

f.close() should be after the loop.

 

Also, you forgot the () on

outToFile.close

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

You close the file while still using it.

 

f.close() should be after the loop.

 

Also, you forgot the () on

outToFile.close

What line does the f.close() belong on?

Link to comment
Share on other sites

Link to post
Share on other sites

The line is fine, just get rid of the indention to take it out of the loop.

Thanks for all of your help. May I keep this thread alive and keep posting issues I have or start a new one each time? Also may I PM you with questions? Like I said I am new at programming. 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for all of your help. May I keep this thread alive and keep posting issues I have or start a new one each time? Also may I PM you with questions? Like I said I am new at programming. 

If the problems aren't related, it is better to make different threads (mainly because your problem may already have been asked and answered, and it will be easier for the future to have threads dedicated to specific problems).

 

@fizzlesticks can speak for himself, but if you want to PM me, that's fine.

[spoiler=My Current PC]AMD FX-8320 @ 4.2 Ghz | Xigmatek Dark Knight Night Hawk II | Gigabyte GA-990FXA-UD3 | 8GB Adata XPG V2 Silver 1600 Mhz RAM | Gigabyte 3X Windforce GTX 770 4GB @ 1.27 Ghz/7.25 Ghz | Rosewill Hive 550W Bronze PSU | Fractal Design Arc Midi R2 | Samsung Evo 250 GB SSD | Seagate Barracuda 1TB HDD | ASUS VS239H-P | Razer Deathadder 2013 Partlist

 

LTT Build-Off Thread: http://linustechtips.com/main/topic/35226-the-ltt-build-off-thread-no-building-required/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×