Python Forum

Full Version: Excel file in use from Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all! My program writes to three .csv files. At the end of the program, I do a filename.close() for all three. Usually, this does the trick and after the program executes I'm able to open these files in Excel and look at them.

Intermittently, I get the "file is locked for editing by 'another user.' Open 'Read-Only' or click 'Notify' to open read-only and receive notification when the document is no longer in use."

Now... the first few times this happened would be when an exception was raised and the program stopped. In these cases, I'm able to manually enter filename.close() in JN and the file opens fine.

With this intermittent issue, though, it's not following any exception and manually entering filename.close() in JN does not unlock the file. Twice now, it seems, the file is unlocked not when I close the two JN browser tabs but rather when I close Anaconda Powershell Prompt, which is always running in the background when I'm using JN.

Any ideas on what I can do to avoid this?
Mark17 Wrote:Any ideas on what I can do to avoid this?
Please show code.
Without using filename = open(filename, 'w')

Try to use With:

with open(filename, 'w') as file:
    file.write('text')
This way you don't have to filename.close(). This way is more efficient and automatically closes the file if an error is raised.
(Jun-29-2022, 01:42 PM)Oshadha Wrote: [ -> ]Without using filename = open(filename, 'w')

Try to use With:

with open(filename, 'w') as file:
    file.write('text')
This way you don't have to filename.close(). This way is more efficient and automatically closes the file if an error is raised.

Does the entire program need to go inside this with block, then, or would I just include this in those places I'm actually writing to the file?
(Jun-30-2022, 01:21 PM)Mark17 Wrote: [ -> ]
(Jun-29-2022, 01:42 PM)Oshadha Wrote: [ -> ]Without using filename = open(filename, 'w')

Try to use With:

with open(filename, 'w') as file:
    file.write('text')
This way you don't have to filename.close(). This way is more efficient and automatically closes the file if an error is raised.

Does the entire program need to go inside this with block, then, or would I just include this in those places I'm actually writing to the file?

All code that requires the file text wrapper (file variable).
Basically only when writing / reading the file.
(Jul-03-2022, 05:45 AM)Oshadha Wrote: [ -> ]
(Jun-30-2022, 01:21 PM)Mark17 Wrote: [ -> ]Does the entire program need to go inside this with block, then, or would I just include this in those places I'm actually writing to the file?

All code that requires the file text wrapper (file variable).
Basically only when writing / reading the file.

Not sure I totally follow but I'll fiddle around with it a bit and come back later if I still have questions. Thanks!
This creates a context where the file is open.
with open(filename, 'w') as file:
    file.write('text')
The file only remains open while in this context. The context ends when you leave the code block. You leave the code block when you de-indent.
def func(arg):
    # Assumes caller's context.  So file still open.
    pass

with open(filename, 'w') as file:
    file.write('text')
    # File is still open
    func(file)  # Called function executes in current context.
    # File is still open
# De-indent ends block of code.  File is closed
If you have a lot of code that must execute with the file open I would put that code inside a function and call the function from inside the context manager as shown above.
(Jul-04-2022, 03:58 AM)deanhystad Wrote: [ -> ]This creates a context where the file is open.
with open(filename, 'w') as file:
    file.write('text')
The file only remains open while in this context. The context ends when you leave the code block. You leave the code block when you de-indent.
def func(arg):
    # Assumes caller's context.  So file still open.
    pass

with open(filename, 'w') as file:
    file.write('text')
    # File is still open
    func(file)  # Called function executes in current context.
    # File is still open
# De-indent ends block of code.  File is closed
If you have a lot of code that must execute with the file open I would put that code inside a function and call the function from inside the context manager as shown above.

That clarifies. Thank you!