Python Forum
Excel file in use from Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Excel file in use from Python
#1
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?
Reply
#2
Mark17 Wrote:Any ideas on what I can do to avoid this?
Please show code.
Reply
#3
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.
Mark17 likes this post
Reply
#4
(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?
Reply
#5
(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.
Mark17 likes this post
Reply
#6
(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!
Reply
#7
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.
Mark17 likes this post
Reply
#8
(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!
Oshadha likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 250 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,840 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 773 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 801 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,523 Jan-21-2023, 06:57 AM
Last Post: jacklee26
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,071 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020