So here's an example FeaturePython class that demonstrates the error.
I'm trying to write to the XML in the onDocumentRestored event. Maybe that's not allowed? Nevertheless, I get the same error if I try to open the file for writing from the FreeCAD python console, or even the python console launched from a terminal window.
I get the error following the steps below in the Python console:
1. Call create()
2. Save the file
3. Close the file, then reopen it.
Notes:
- Step 1 should create 'my_file.txt' in the user home directory
- Step 3 tries to write to the transient file location, which triggers the 'Permission Denied' error.
Code: Select all
import datetime
import FreeCAD as App
def create():
file_path = App.getHomePath() + 'my_file.txt'
with open(file_path, ''w', encoding='UTF-8') as _f:
_f.write('Initial write at ' + str(datetime.datetime.now()))
obj = App.ActiveDocument.addObject("App::FeaturePython", 'TransientFpo')
fpo = TransientFpo(obj)
obj.filepath = file_path
class TransientFpo():
def __init__(self, obj):
"""
Default Constructor
"""
obj.Proxy = self
self.Type = "TransientFpo"
self.Object = obj
obj.addProperty(
'App::PropertyFileIncluded', 'filepath', '').filepath = ''
self.init = True
def __getstate__(self):
return self.Type
def __setstate__(self, state):
if state:
self.Type = state
def onDocumentRestored(self, obj):
with open(App.ActiveDocument.TransientDir + '/my_file.txt', 'w', encoding='UTF-8') as _f:
_f.write('updated at ' + str(datetime.datetime.now()))