简体   繁体   English

Subprocess.call() cwd 不能从文本加载值设置

[英]Subprocess.call() cwd cannot set from text loaded value

TL;DR TL; 博士

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. subprocess.call(cwd=filepath)在我从文本文件设置filepath变量时不起作用,但是当我使用相同的路径手动设置它时起作用。

More Info更多信息

When I use subprocess.call I specify the cwd of the command with a string variable.当我使用subprocess.call时,我使用字符串变量指定命令的cwd 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.但是,我想从文本文件中的值加载 cwd 路径。 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.cwd=filepathfilepath设置为从文本文件加载的字符串值时,我收到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.最初,我只是从没有 pathlib 元素的文件中拉出字符串。 I've tried using just pathlib without converting it to a string as well.我试过只使用 pathlib 而不将其转换为字符串。 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/ BPL-C:/程序文件/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!谢谢!

For anyone else with this same issue, add .rstring() to the end of your string.对于遇到同样问题的其他人,请将 .rstring() 添加到字符串的末尾。 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())更新后的行内容为: blenderfilepath = str(pathlib.Path(bfp[1]).rstring())

Thank you to jasonharper for the help!感谢jasonharper的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM