Can't import filenames with blanks in - Rhino7, python

Hi
Hope everyone is doing well.

Running Python script in Rhino 7.
I can’t get import to accept blanks as part of the file name. No matter what I do it uses first blank as
an enter and chops the the name off after the “~1”.
I’ve seen other examples where chr(34) was used but it was for vb.

print “xxxxxxxx”
fname = “~1 10 3.35 x 4.55.stl”
command = “-_import " + “’” + rs.WorkingFolder() + “\” + “stones” + ‘\’ + fname + “’” + " -_enter”
print "My current directory is : " + command
rs.Command(command)

I typed “python equivalent to chr(34) in vb” into google and got some links back that looked like they could help you.

Hi
Thanks for your help. I didn’t see anything that would work. I think the issue is how Rhino treats a blank in
the command line and how to get Rhino to ignore the blank in the filename. In the vb sample I found
using chr(34) around the file name forced Rhino to look past the blanks but that option doesn’t look like it works using python.

OK!
I got it to work. I don’t know why it did not work before.

command = “-_import " + chr(34) + rs.WorkingFolder() + “\” + “stones” + ‘\’ + fname + chr(34) + " -_enter”

Instead of using char(34) you can also escaped double-quotes \". Further, os.path.join to make the path code work cross-platform

import os
import os.path
import rhinoscriptsyntax as rs

fname = "test file name.txt"
filename = os.path.join(rs.WorkingFolder(), "stones", fname)
command = "-_import \"{}\" _enter".format(filename)

print(command)
1 Like

You can also take advantage of Python’s use of both single quotes and double quotes -

command = '-_import "{}" _enter'.format(filename)

Although I got some flack for that from someone, I don’t recall why anymore.

1 Like

That’s fine too I guess. I could imagine someone not liking mixing these, but if it works for you… I suppose representing strings in a mixed way ( a = "one", b = 'two') in code is what one would trip over. But who knows. :man_shrugging:

It worked
much cleaner

Thanks

I like your method too.

Both these solution gave a lot of info.

Thanks