Space on path

Hello,

I have this simple code to import some file to rhino, the path is correct
but “Program Files” have a space, this cause problems on comand
import, there is any way to sove this?

path = “C:\Program Files\data\teste\file.3dm”

commandline = “_-Import " + path + " _Enter”

ok = rs.Command(commandline)

thank you

Hi @MatrixRatrix,

Try:

path = "C:\\Program Files\\data\\teste\\file.3dm"
path = "\"" + path + "\"" # Escape the double quotes
print path

– Dale

2 Likes

Hello Dale,

The problem is not //, the problem this the space between “Program” and “Files”, this space
cause path interruption.

“C:\Program.3dm not found, unable open”

Thanks

Note the espaced double quotes in the sample from @dale. Those will ensure proper path.

OK, I was doing rong, is solve thank you.

hello!
i’ve got the same problem…
in this code:

path=rs.DocumentPath()

if in path, there is a space, it will interrupt the script, how can i do?

Enclose the path in double quotes in the string.

This command return document path.

For me is working, even with spaces on path name.

Sure you’ll get a path with spaces back. The point is to enclose it properly when using it as part of a macro.

path = rs.DocumentPath()

enclosed_path = "\"%s\"" % (path)
1 Like

Ok!! it is working!!!
but what does mean

"\"%s\"" %

That is a string that contains two escaped double-quotes \" and a format variable %s. These are as string literal inside double-quotes → "\"%s\"". The percentage that after this bit comes means “replace with what we get in parentheses next”. So the value of variable path replaces the %s in the formatted string. %s means the value given should be a string.

Clearer would actually be:

enclosed_path = "\"{}\"".format(path)

The first I gave is so-called old-style formatting (https://docs.python.org/2/tutorial/inputoutput.html#old-string-formatting), but you probably should go for the fancier new-style. Scroll up a bit in the page I linked for old-style formatting.

1 Like

This is also works for Rhinocommon.