简体   繁体   中英

Close already open csv in Python

Is there a way for Python to close that the file is already open file.

Or at the very least display a popup that file is open or a custom written error message popup for permission error.

As to avoid:

PermissionError: [Errno 13] Permission denied: 'C:\\zf.csv'

I've seen a lot of solutions that open a file then close it through python. But in my case. Lets say I left my csv open and then tried to run the job.

How can I make it so it closes the currently opened csv?

I've tried the below variations but none seem to work as they expect that I have already opened the csv at an earlier point through python. I suspect I'm over complicating this.

f = 'C:\\zf.csv'
file.close()
AttributeError: 'str' object has no attribute 'close'

This gives an error as there is no reference to opening of file but simply strings.

Or even..

theFile = open(f)
file_content = theFile.read()
# do whatever you need to do
theFile.close()

As well as:

fileobj=open('C:\\zf.csv',"wb+")

if not fileobj.closed:
    print("file is already opened")

How do I close an already open csv?

The only workaround I can think of would be to add a messagebox, though I can't seem to get it to detect the file.

filename = "C:\\zf.csv"
if not os.access(filename, os.W_OK):
    print("Write access not permitted on %s" % filename)
    messagebox.showinfo("Title", "Close your CSV")

Try using a with context, which will manage the close ( __exit__ ) operation smoothly at the end of the context:

with open(...) as theFile:
    file_content = theFile.read()

You can also try to copy the file to a temporary file, and open/close/remove it at will. It requires that you have read access to the original, though.

In this example I have a file "test.txt" that is write-only (chmod 444) and it throws a "Permission denied" error if I try writing to it directly. I copy it to a temporary file that has "777" rights so that I can do what I want with it:

import tempfile, shutil, os

def create_temporary_copy(path):
    temp_dir = tempfile.gettempdir()
    temp_path = os.path.join(temp_dir, 'temp_file_name')
    os.chmod(temp_path, 0o777);          # give full access to the tempfile so we can copy
    shutil.copy2(path, temp_path)        # copy the original into the temp one
    os.chmod(temp_path, 0o777);          # replace permissions from the original file
    return temp_path

path = "./test.txt"                      # original file
copy_path = create_temporary_copy(path)  # temp copy
with open(copy_path, "w") as g:          # can do what I want with it
    g.write("TEST\n")
f = open("C:/Users/amol/Downloads/result.csv", "r")
print(f.readlines()) #just to check file is open
f.close()
# here you can add above print statement to check if file is closed or not. I am using python 3.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM