Create file from Python WB

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
project4
Posts: 237
Joined: Fri Jul 12, 2013 12:53 pm

Create file from Python WB

Post by project4 »

Hi guys,

I'm involved in WorkBench development written in Python.
For debug purposes I need to create a file from within the python code.
Is there a way to do it?

I've tried to call open("filename", "w"), but got "Read-only file system" exception...

If not, is there any other ways to output data beside the FreeCAD.Console.PrintMessage?

Thanks.
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: Create file from Python WB

Post by easyw-fc »

project4 wrote: Thu Jul 19, 2018 5:55 pm Hi guys,

I'm involved in WorkBench development written in Python.
For debug purposes I need to create a file from within the python code.
Is there a way to do it?

I've tried to call open("filename", "w"), but got "Read-only file system" exception...

If not, is there any other ways to output data beside the FreeCAD.Console.PrintMessage?

Thanks.
here a sample code for writing a file to home folder

Code: Select all

import FreeCAD, os, sys
from os.path import expanduser

home = expanduser("~")
out_file=os.path.join(home,'myfile.txt')
FreeCAD.Console.PrintMessage (out_file)
FreeCAD.Console.PrintMessage ('\n')
mycontent=['Hello','this is a second line','and then a third']
with open(out_file, 'w') as my_file:
    for item in mycontent:
        my_file.write("%s\n" % item)

you need to define the folder in which you are going to write the file.
And the folder must be writable
Post Reply