David Hawkins Posted November 12, 2015 Share Posted November 12, 2015 I'm trying to write a script to read data from a text file in Vectorworks. The help file seems curiously incomplete in this area and some of the Python examples appear to be incorrect. Still, it appears straightforward and I'm at a bit of a loss as to why my code isn't working. My code is based on the 'GetFile' example code in the function reference, and it boils down to this: import vs fileName = vs.GetFile() a = vs.Read() vs.Close(fileName) (in VW2016 on OS X) I would expect this to result in the first number or word in my chosen file being read into variable a. Running this script in VW2016 produces a file-choosing dialog box as expected. It then produces an error as follows: File "", line 3, in AttributeError: 'module' object has no attribute 'Read' Can anyone shed any light on this? or point me at a working example of how to read from a text file in vectorworks? Quote Link to comment
DomC Posted November 12, 2015 Share Posted November 12, 2015 (edited) Hi David Hm, I just hav experience by using python moduls for text handling. Because you can get help for this via google for every error message. Also with python you can do so much more (sort, arrange, split, sorting out characters etc). An Example. Which Reads a text fila as one single string value. To get File Dialog use vs.GetFile, after that you should convert the path (Which is MacHD:User:Dom:etc:ect) to a Posix Path (MacHD/User/Dom/etc/etc) Try this: import vs fileName = vs.GetFile() bool, fileName=vs.ConvertHSF2PosixPath(fileName) a=open(fileName).read() vs.Message(a) Edited November 12, 2015 by DomC Quote Link to comment
JBenghiat Posted November 12, 2015 Share Posted November 12, 2015 The VS file routines have a number of limitations, so they may actually be disabled in the Python API. Follow the above advice and use Python's built-in routines for file handling. The only built-in Python routines I don't recommend are UI -- tk, etc. The Python library needs a special update for Mac, and the Python commands can cause some memory issues when run inside VW, so you are best using the VW dialogs. -Josh Quote Link to comment
David Hawkins Posted November 13, 2015 Author Share Posted November 13, 2015 Thank you both very much for this excellent and speedy advice, DomC's code does the trick. Now if only the Vectorworks function reference said something to the effect of "Read() is not included in the Vectorworks Python module. Python provides its own file i/o functions, see example below..." Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.