Python Forum
Keep a file locked for the life of an application
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keep a file locked for the life of an application
#1
Suppose I have a file I'm reading from and I want to keep it open for the life of the application, until the users quits. Using Python under Windows, is it possible?

Essentially what i want to do is what any word processor does, once a file is open in the word processor, it's locked as long as the word processor is open, and if you attempt to delete/move it, the OS will generate an error usually along the lines of "This file cannot be removed, it's being used by (program name here)"
Reply
#2
def foo():
    pass

def bar():
    pass
if __name__ == "__main__":
    f = open('file.txt')

    # do something
   
    sys.exit(f.close())
For instance...
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
On not-windows, you have the fcntl module which can lock files (or parts of files): https://docs.python.org/3/library/fcntl....cntl.flock
On windows, the same functionality can be found in the msvcrt module: https://docs.python.org/3.6/library/msvc...rt.locking
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to send data from a python application to an external application aditya_rajiv 1 2,173 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  Optimize a game of life (4 sec for python when 6 ms for C version !) fatango 0 1,665 Jan-06-2021, 09:13 PM
Last Post: fatango
  How to assess elapsed time to execute a .exe file or opening a desktop application? Sudershan 2 2,135 Apr-18-2020, 01:40 PM
Last Post: buran
  Life counter not working joshp2709 1 1,677 Oct-05-2019, 10:37 AM
Last Post: Yoriz
  Open a locked ZIP file on OSX tomywd 1 2,231 Mar-13-2018, 04:14 AM
Last Post: micseydel
  Print the file using the associated application on Linux Borisko 1 3,657 Oct-15-2017, 07:14 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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