0

TL;DR

subprocess.call(cwd=filepath) does not work when I set the filepath variable from a text file, but does work when I set it manually using an identical path.

More Info

When I use subprocess.call I specify the cwd of the command with a string variable. When the string is manually defined, everything works how it should. However, I want to load the cwd path from a value within a text file. I have that part nailed down as well, and I am loading the correct value from the text file. When cwd=filepath and filepath is set to the string value loaded in from the text file, I get a NotADirectoryError: [WinError 267] The directory name is invalid. Keep in mind that if I set the variable manually to the exact same path, I do not get this error. I think this is some kind of formatting issue, and I've played around with it/looked around the internet for a few days, but haven't found a working solution.

Full Code

import subprocess # to run the process.
import pathlib #to get the path of the file.

programpath = str(pathlib.WindowsPath(__file__).parent.absolute())
blenderfilepath = 'C:/Program Files/Blender Foundation/Blender 2.81/'
settingsfile = 'settings'

# Load in the variables from settings.
def Load():
    global blenderfilepath
    # # look inside settings file for settings.
    sf = open(programpath + '\\' + settingsfile, 'r')
    for line in sf:
        if 'BPL' in line:
            bfp = line.split('-', maxsplit=1)
            blenderfilepath = str(pathlib.Path(bfp[1]))
            print('Path loaded for Blender: ' + blenderfilepath)

        else:
            print('Using default config...')
            return

    sf.close()
    print('Settings loaded')

# Run next job executes the command to run the next job.
def RunNextJob():
    print('Running next job...')
    print(blenderfilepath)
    currentjob = subprocess.call('blender.exe', cwd=blenderfilepath, shell=True, stdout=subprocess.PIPE)

RunNextJob()

Additional Information and Thanks!

Initially, I was just pulling the string out of the file with no pathlib element. I've tried using just pathlib without converting it to a string as well. This is notable to mention.

For additional context, the "settings" file is one line that contains one line:

BPL-C:/Program Files/Blender Foundation/Blender 2.81/

It is parsed through to extract the path. I have validated that the path is extracted correctly.

Any help is appreciated. Thanks!

Monty
  • 1
  • The line read from the text file is going to be terminated by a newline character. You need to strip that off before using it. – jasonharper Jan 29 '20 at 06:32
  • Thanks for the reply. Your comment led me to use .rstring(), which fixed the problem. Many thanks! – Monty Jan 30 '20 at 23:18

1 Answers1

0

For anyone else with this same issue, add .rstring() to the end of your string. It will strip off any line endings and other sometimes invisible elements in strings.

The updated line reads: blenderfilepath = str(pathlib.Path(bfp[1]).rstring())

Thank you to jasonharper for the help!

Monty
  • 1