Home > Software engineering >  I don't know how to open a file and I get an error ValueError: I/O operation on closed file [cl
I don't know how to open a file and I get an error ValueError: I/O operation on closed file [cl

Time:09-17

with open('play.txt', 'w') as csv_file:
            csv_reader = csv.reader(csv_file)

        for line in csv_reader:
            print("First game, first court: "   line[0]   " and"   line[1]   " against "   line[2]   " and"   line[
                3]   ".")
            print("First game, second court: "   line[4]   " and"   line[5]   " against "   line[6]   " and"   line[
                7]   ".")
            print(''
                '')

            print("Second game, first court: "   line[0]   " and"   line[2]   " against "   line[1]   " and"   line[
                4]   ".")
            print("Second game, second court: "   line[4]   " and"   line[6]   " against "   line[5]   " and"   line[
                7]   ".")
            print(''
                '')

            print("Third game, first court: "   line[0]   " and"   line[7]   " against "   line[6]   " and"   line[
                1]   ".")
            print("Third game, second court: "   line[2]   " and"   line[5]   " against "   line[4]   " and"   line[
                3]   ".")
            print(''
                '')

Error: ValueError: I/O operation on closed file. It's a KIVY app which takes 8 arguments with entry box.

CodePudding user response:

Indent your for loop correctly. It should be inside the 'with' block at same level as csv_reader = csv.reader(csv_file) statement. Anything outside 'with' block means file is closed. Furthermore, I think the mode argument in the open function should be 'r', for reading the file.

with open('play.txt', 'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:

CodePudding user response:

The problem here is that you tried to read from the file but instead of "read mode" you are using "write mode". If you are using "w" that means you should only use the file for writing in it. If you try to read from it you'll get an error that the operation is not supported. So, instead of "w" mode, you should use "r". Or if you need to read from the file and write into the file you can use "a " mode. Something like this:

with open('play.txt', 'a ') as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:
            print("First game, first court: "   line[0]   " and"   line[1]   " against "   line[2]   " and"   line[
                3]   ".")
            print("First game, second court: "   line[4]   " and"   line[5]   " against "   line[6]   " and"   line[
                7]   ".")
            print(''
                '')

            print("Second game, first court: "   line[0]   " and"   line[2]   " against "   line[1]   " and"   line[
                4]   ".")
            print("Second game, second court: "   line[4]   " and"   line[6]   " against "   line[5]   " and"   line[
                7]   ".")
            print(''
                '')

            print("Third game, first court: "   line[0]   " and"   line[7]   " against "   line[6]   " and"   line[
                1]   ".")
            print("Third game, second court: "   line[2]   " and"   line[5]   " against "   line[4]   " and"   line[
                3]   ".")
            print(''
                '')

CodePudding user response:

Try checking the indentation to do with the for loop, or the csv_reader so either increase the for loops indentation or decrease the csv_readers indentation.

with open('play.txt', 'w') as csv_file:
        csv_reader = csv.reader(csv_file)

        for line in csv_reader:
            print("First game, first court: "   line[0]   " and"   line[1]   " against "   line[2]   " and"   line[
                3]   ".")
            print("First game, second court: "   line[4]   " and"   line[5]   " against "   line[6]   " and"   line[
                7]   ".")
            print(''
                '')

            print("Second game, first court: "   line[0]   " and"   line[2]   " against "   line[1]   " and"   line[
                4]   ".")
            print("Second game, second court: "   line[4]   " and"   line[6]   " against "   line[5]   " and"   line[
                7]   ".")
            print(''
                '')

            print("Third game, first court: "   line[0]   " and"   line[7]   " against "   line[6]   " and"   line[
                1]   ".")
            print("Third game, second court: "   line[2]   " and"   line[5]   " against "   line[4]   " and"   line[
                3]   ".")
            print(''
                '')

CodePudding user response:

    with open('play.csv', 'rt ') as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:
            print("First game, first court: "   line[0]   " and"   line[1]   " against "   line[2]   " and"   line[3]   ".")
            print(
                "First game, second court: "   line[4]   " and"   line[5]   " against "   line[6]   " and"   line[7]   ".")
    
            print(
                "Second game, first court: "   line[0]   " and"   line[2]   " against "   line[1]   " and"   line[4]   ".")
            print(
                "Second game, second court: "   line[4]   " and"   line[6]   " against "   line[5]   " and"   line[7]   ".")
    
            print("Third game, first court: "   line[0]   " and"   line[7]   " against "   line[6]   " and"   line[1]   ".")
            print(
                "Third game, second court: "   line[2]   " and"   line[5]   " against "   line[4]   " and"   line[3]   ".")
  • Related