On my job, I am working with a big .NET application which writes to a log file. Let’s call the application CompanyApplication. I have written a simple Python script which clears the log:
file_object = open('C:\\log.txt', 'w')
file_object.write("")
file_object.close()
When CompanyApplication.exe is not running this works fine. However, when CompanyApplication.exe is running I get this error:
Traceback (most recent call last):
File “deleteLog.py”, line 1, in <module>
file_object = open(‘C:\log.txt’, ‘w’)
IOError: [Errno 13] Permission denied: ‘C:\log.txt’
This must be because CompanyApplication holds a lock on the log file. Is there any way I can “unlock” the log file, clear it, and then “hand the lock back” to CompanyApplication? I would prefer a solution which could be automated (that’s why I wrote the Python script in the first place).
Additional info: There is only so much I can do to change CompanyApplication itself. I am using Windows XP Pro. I have administrator privileges.
You need to fill out the FileShare parameter for the open call on the log file in your .NET program.
Specifically you will want to allow read/write sharing.
Depending on how your .NET application works when writing to the log though, what you are suggesting might not work as expected and may cause problems with the .NET program.
You could simply start a thread in the .NET program which creates an event and waits on it. Later you can set that event from your Python program and the .NET will safely clear it’s own log file and reset the event.