Reputation: 2051
I am writing a utility in Python that may require a user to open a file while the Python program is writing to it.
The file is open as so in the Python program: CSV = open(test.csv, "a")
When the Python program is running and the user double clicks test.csv
in the OS, the Python program halts with the following error:
PermissionError: [Errno 13] Permission denied: 'test.csv'
I understand why this happens. I am wondering if there is something I can do so that the Python program can still write to the file while the file (a read-only copy perhaps) is open. I've noticed in the past that Perl programs, for example, still write to a file while it is open, so I know that this is programmatically possible. Can this be done in Python?
Upvotes: 3
Views: 1535
Reputation: 2051
The best solution I've found is to make a copy of the file and then open it if you wish to view the contents of the file while it is being written to. It is easy to make a copy of a file programmatically if you wish to automate the process.
If one wishes to implement a feature where the user can see the file as it is updated in real time, it is best to use communicate the data to a receive through a separate method, possibly by sockets or simply to stdout.
Upvotes: 1