Sjn73
Sjn73

Reputation: 189

Accessing global file in the user defined function

I am trying to access file which is opened globally as below:

with open("number.txt") as num:
    val=int(num.read())

consider number.txt will be having some number After the above lines of code if I use the above file pointer inside a method as below:

def updateval():
    global num
    num.write(numb)

I am getting error as :ValueError: I/O operation on closed file If I open file inside function then there wont be any issue.But I want to open file only once so in many opther function I can perform file operation without opening again and again

Please help me in resolving this!! Many thanks in advance..

Upvotes: 1

Views: 694

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177640

You have two problems:

  1. with closes the file when the enclosing code block exists.
  2. You open the file for reading, so even if you don't close the file, you can't write to it.

Solution 1:

with open('number.txt') as num:  # opens for read-only by default
    val = int(num.read())
# file is closed here

def updateval(numb):
    with output('number.txt','w') as num:  # re-opened for writing
        num.write(numb)
    # file is closed here

Solution 2 (if you really want to open the file once):

num = open('number.txt','r+')  # Open for reading and updating.
val = int(num.read())

def updateval(numb):
    # You don't need "global" when you mutate an object,
    # only for new assignment, e.g. num = open(...)
    num.seek(0)           # make sure to write at the beginning of the file.
    num.truncate()        # erase the current content.
    num.write(str(numb))  # write the number as a string
    num.flush()           # make sure to flush it to disk.

Obviously, the second solution you have to micromanage what you are doing. Use solution 1.

Upvotes: 1

Joe Iddon
Joe Iddon

Reputation: 20414

The whole point of a with statement is that the file is closed when you leave that scope. The advantage of this is that if you get errors, the file is closed properly.

So when you do the following:

with open("number.txt") as num:
    val=int(num.read())

you assign the contents of number.txt to val and then close the file.

So therefore, when you try and write to it later (no matter if you are in a function or not), you will get the error that you posted in the question:

ValueError: I/O operation on closed file

So if you really want to be able to write to the file inside any function without having to keep opening it, you just have to not use a with statement, just open the file for writing ('w') explicitly and close it at the end of the program.

So the main outline of your code would look something like:

with open("number.txt") as num:
    val=int(num.read())

file = open("number.txt", "w")

def writeToFile(data):
    global file
    #more code
    file.write(data)
    #more code

#more code

file.close()

note this is not good practice, you should pass the file object into the function and remove the use of global variables

Upvotes: 0

Related Questions