anytree icon indicating copy to clipboard operation
anytree copied to clipboard

RenderTreeGraph(tree).to_picture("test.png") Error: permission denied

Open jhaerig opened this issue 4 months ago • 1 comments

Found a problem when trying to use RenderTreeGraph on a tree structure.

>>> anytree.__version__
'2.12.1'
from anytree import Node, RenderTree
from anytree.dotexport import RenderTreeGraph
tree = Node("root")
b = Node("Branch1", parent=tree)
c = Node("Branch2", parent=tree)
d = Node("SubBranch1",parent=c)
e = Node("SubBranch2",parent=c)
RenderTreeGraph(tree).to_picture("tree.png")

Error

Error: dot: can't open C:\Users\<user>\AppData\Local\Temp\tmpzb2ruk5a: Permission denied
Traceback (most recent call last):
  File "C:\Users\<user>\Desktop\PythonScripts\anytree_test.py", line 8, in <module>
    RenderTreeGraph(tree).to_picture("tree.png")
  File "C:\Python39\lib\site-packages\anytree\exporter\dotexporter.py", line 308, in to_picture
    check_call(cmd)
  File "C:\Python39\lib\subprocess.py", line 373, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['dot', 'C:\\Users\\<user>\\AppData\\Local\\Temp\\tmpzb2ruk5a', '-T', 'png', '-o', 'tree.png']' returned non-zero exit status 2

I found the reason for that issue in: https://github.com/c0fec0de/anytree/blob/65a5d09ce0a592a80918f094ec3fa48b7faca250/anytree/exporter/dotexporter.py#L302C1-L308C28

Current

with NamedTemporaryFile("wb", delete=False) as dotfile:
	dotfilename = dotfile.name
	for line in self:
		dotfile.write(("%s\n" % line).encode("utf-8"))
	dotfile.flush()
	cmd = ["dot", dotfilename, "-T", fileformat, "-o", filename]
	check_call(cmd)

Should be

with NamedTemporaryFile("wb", delete=False) as dotfile:
	dotfilename = dotfile.name
	for line in self:
		dotfile.write(("%s\n" % line).encode("utf-8"))
	dotfile.flush()
cmd = ["dot", dotfilename, "-T", fileformat, "-o", filename]
check_call(cmd)

jhaerig avatar Oct 15 '24 12:10 jhaerig