Batch processing files with a space in their path

I’m hoping to create a script that can search a folder for all files ending in .obj and apply ReduceMesh to them (I have a large number of files to do this to).

So far my script is as so:

import os
import rhinoscriptsyntax as rs

def getObjHelper(dir_path, matches):
    for root, dirnames, filenames in os.walk(dir_path):
        for file in filenames:
            if file.endswith('.obj'):
                matches.append(os.path.join(root, file))
        for dir in dirnames:
            getObjHelper(dir, matches)
    return matches

def getObjFiles(dir_path):
    matches = []
    for root, dirnames, filenames in os.walk(dir_path):
        for file in filenames:
            if file.endswith('.obj'):
                matches.append(os.path.join(root, file))
        for dir in dirnames:
            getObjHelper(dir, matches)
    return matches
                   
def reduceFileSize(filepath):
    filepath = filepath.replace(' ', "\\ ")
    rs.DocumentModified(False)
    print("Opening {0}".format(filepath))
    rs.Command('_-Open {0} _Enter'.format(filepath))
    rs.UnselectAllObjects()
    rs.SelectObjects(id)    
    #rs.Command('_-ReduceMesh"')
    #rs.Command('_-Export "' + filepath + _Enter _Enter')
    
    #rs.UnselectAllObjects()

if __name__ == "__main__":
    root_path = rs.BrowseForFolder()

    obj_files = getObjFiles(root_path)
    for file in obj_files:
        reduceFileSize(file)

So far getObjFiles() works as expected and I get a list of all .obj files. However, when I run the _-Open command with rs.Command(), I get an error where the filepath is truncated at the first space.

I’ve tried a handful of different methods to prepare the filepath string, but none seem to work – the script always cause issues at the first space. Given that these paths are automatically found using os.listdir and os.walk, I’m not sure what the best way to fix them is.

An example of the error I get, for selecting the folder E:\Physical Models\models\ is:

Opening E:\Physical\ Models\models\cat_stage01_model.obj
>>> ( Debugging=Off ): _-Open
Name of the file to open ( UpdatePromptUpdateBlocks=Yes  Browse ): E:\Physical\
File E:\Physical\.3dm.3dm not found, unable to open

Any thoughts on how to do this?

thanks!

Use

rs.Command('_-Open "{0}" _Enter'.format(filepath))
1 Like

Thank you for this answer as it resolved my issue.

I have a follow up question if you don’t mind providing some more information or pointing me to the documentation that can explain.

My question is what is the reasoning that adding “{0}” with the .format(filepath) allow you to have spaces in the path without triggering the command?

TIA

The format statement is not actually necessary.

rs.Command('_-Open "{0}" _Enter'.format(filepath))

could also be

rs.Command('_-Open "filepath" _Enter')

What is necessary is to have the file path itself enclosed in quotes as it might have file or folder names with spaces in them - any space will be interpreted as Enter, the Open command will start right there and so the rest of the file path will be ignored.

The thing is that rs.Command() is looking for a string and as such also needs to be enclosed in quotes. So you have the situation where you need nested quotes. If they are both single quotes or both double quotes, it will fail - as it will then separate into two strings. With Python, as both single quotes and double quotes are accepted and are not considered the same thing, that becomes a useful workaround - enclose the whole command string in single quotes and inside that enclose the file path in double quotes.

Does that make sense?