python - trying to create a list with numbers from an infile to a outfile -


def main():      infilename = input("insert list file: ")     outfilename = input("insert new list: ")      infile = open(infilename, "r")     todolist = infile.readlines()      outfile = open(outfilename, "w")      in range(len(todolist)):         print(todolist[i], file = outfile)          infile.close()         outfile.close()     print("new list prints to",outfilename)  main() 

i keep getting error message saying print(todolist[i], file = outfile) valueerror: i/o operation on closed file. please help.

all i'm trying take todo list file listed as

do hw
throw away trash
shower dog

and want is

1. hw 2. throw away trash 3. shower dog 

you have indentation error:

for in range(len(todolist)):     print(todolist[i], file = outfile)      infile.close()     outfile.close() 

correct this:

for in range(len(todolist)):     print(todolist[i], file = outfile)  infile.close() outfile.close() 

and if want add numbers @ line beginnings, should this:

for in range(len(todolist)):         outfile.write("{0}. {1}".format(str(i), todolist[i]))  

Comments

Popular posts from this blog

css - I want to align grid in center -

Contact Form PHP Email Script -

python - SWIG function not printing output -