1

There is a software which writes/appends some log files. I wrote a Python script to read those log files and copy the info in another file.

How can I make sure that the Python script won't interrupt the softwares process or that the Python script won't crush when trying to access the files?

D D
  • 31
  • 3
  • 1
    Literally copy-pasting my comment from another question today: Writing/opening an open file is an OS-level limitation. There is nothing that Python (or any other language) can do about it (and that's a good thing) – DeepSpace Oct 22 '18 at 13:42
  • 1
    *Accessing* the files won't necessarily be an issue if another process has it locked. As long as you `open()` it as read-only mode (`r`) it shouldn't interfere with the other process whatsoever. Writing to the file (`w`) however will at the OS's mercy per above comment. – r.ook Oct 22 '18 at 13:43
  • @DeepSpace is that even the case if the application opens the file with a share-read setting? – Patrick Artner Oct 22 '18 at 13:43
  • If you make sure the Python script only ever opens the file in `r` (reading) mode you *may* get away with it, but you'll be pushing it. – DeepSpace Oct 22 '18 at 13:44
  • @PatrickArtner I suppose this will be up to the specific OS implementation – DeepSpace Oct 22 '18 at 13:44
  • @DeepSpace I don't imagine opening it in `r` mode will cause any issue. As the question indicated, it's meant to read from the file and copy to another file. It will matter if *the other file* is currently write-locked by another process though. – r.ook Oct 22 '18 at 13:45

1 Answers1

0

Open your file in read only r mode otherwise the if you try to open it in other mode they the chances of Python interrupting the process will depends on the OS and that software which is using that file.

realmanusharma
  • 330
  • 2
  • 10