Create folder to export files in rhino python


#1

Hello everyone.

how can I create a folder to export different type of files in rhino python.

right at the start of the script I want to pront the user to select a location and create a folder, that folder will then be used to save the files the script will export.

it will be 2 .csv files and 1 .dwg file

I tried using rs.savefilename but that creates a file, not a folder.

at the moment I’m exporting just by using:

with open(“filename” + ‘.csv’,“w”) as f:
f.writelines(make_csv(matrix))

but this just exports the file in the same place I have the script saved.

can anyone help me out please
thanks so much


(Menno Deij - van Rijswijk) #2

To create a directory with Python, use os.mkdir
To save there, use os.path.join this will combine the directory name and the filename to an absolute path.

http://docs.python.org/2/library/os.html
http://docs.python.org/2/library/os.path.html#module-os.path

import os
import os.path

dir = r"C:\temp" 
if not os.path.exists(dir):
    os.mkdir(dir)

with open(os.path.join(dir, "filename"+'.csv'), "w") as f:
    f.writelines(make_csv(matrix))

#3

cool, thanks will give it a try.

I was wondering the same but wasn’t sure rhino could import os and os.path

thanks so much


#4

nop didn’t work.

I need to ask the user to select the folder where to save the files.

but I think I can use rs.BrowseForFolder


(Menno Deij - van Rijswijk) #5

os and os.path are default in any Python distribution. So yeah, you can import them.


Python – move a mesh using pythonscript
#6

How could I import a .stl file automatically then export after editing it (( using Python )) ?!