-1

In my assignment, I was supposed to close a file after reading through it in one function. However, I have to access it again from another function later on. It's a requirement to close the file to enforce a good habit of closing files, but I feel like it's unnecessary as I need to open the file again later. Is there a way to close a file and still access it from another function or should I just reopen it manually.

Example Code:

def open(file):

    filename = open(file, "r")
    filename.read()
    filename.close()

def access():

    for line in filename:
        print(line)
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
code_noob
  • 21
  • 5
  • There seems to be an assumption here that opening a file is somehow hard or expensive. As far as file operations go, open is pretty quick. Since this is the second access, everything is cached. – tdelaney Oct 18 '20 at 21:31
  • Even better habit is to use a `with` block for opening and closing files. – khelwood Oct 18 '20 at 21:34
  • That's the thing I don't mind reopening the file again. My teacher is pretty anal about her assignment and if you do something that was not listed in prompt, she'll mark off points. She only expects you to open the file once and close it. Then complete the tasks. – code_noob Oct 18 '20 at 22:14

3 Answers3

1

There's nothing that says you have to close the file before the current function ends, only that you must ensure the file is closed. One way is to pass an already open file to access as an argument, and close it after access returns. For example,

def access1(fh):

    for line in fh:
        print(line)

def access2(fh):
    # Do something else with the file

# This is the builtin open function, not the one in the question
with open(filename) as f:
    access1(f)
    f.seek(0)  # Reset the file pointer for the next function
    access2(f)
chepner
  • 497,756
  • 71
  • 530
  • 681
  • An advantage of writing code this way is that `access` works on any file-like object, increasing the flexibility of the code. – tdelaney Oct 18 '20 at 21:29
  • I think that this answer should expand to two functions that process the file. OP's unfortunately named "open" function processes the file too. That means the file either needs to be reopened or `seek(0)`. – tdelaney Oct 18 '20 at 21:33
  • Yeah, it's not really clear what the point of the OP's `open` is; it doesn't return or otherwise process the data it reads, so it's basically an expensive no-op with a side-effect of putting the file into cache. – chepner Oct 18 '20 at 21:34
  • Agreed, but I think the question is about 2 functions that process the file. I don't think there is any advantage to keeping it open over opening it twice. Your example where the source of the filelike object is decoupled from the processing is good design but I think it would be useful to show that `seek` is part of it. – tdelaney Oct 18 '20 at 21:38
  • I'm ignoring the OP's `open` function until we have a better idea of what. I've updated to show two functions using the same file handle, with the code that opened the file handling the call to `seek`. – chepner Oct 18 '20 at 21:42
  • No it was required to close the file before the function ends. I tried another person's method and it worked for my situation better. But I will be sure to try this if the situation were to come up again. Thanks! – code_noob Oct 18 '20 at 22:09
0

Save the output of filename.read() to a variable. The variable will contain the contents of the file for later use, even if the file is closed.

Seth
  • 2,214
  • 1
  • 7
  • 21
  • Thanks it worked perfectly, at first I tried that but it didn't work don't know why it works now haha. THANKS! – code_noob Oct 18 '20 at 21:28
0

Yes, once closed you need to open the file again. When we are done with performing operations on the file, we need to properly close the file. Closing a file will free up the resources.

Python has a garbage collector to clean up unreferenced objects but keeping the file open is not a good way.

Or we can use

> with open("test.txt", encoding = 'utf-8') as f:    
  # perform file operations

file is closed once we exit the block inside the "with" statement.

kiran_raj_r
  • 116
  • 11