Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法从文本加载的值设置Subprocess.call()cwd_Python_Python 3.x_Subprocess_Blender_Pathlib - Fatal编程技术网

Python 无法从文本加载的值设置Subprocess.call()cwd

Python 无法从文本加载的值设置Subprocess.call()cwd,python,python-3.x,subprocess,blender,pathlib,Python,Python 3.x,Subprocess,Blender,Pathlib,TL;DR subprocess.call(cwd=filepath)在我从文本文件设置filepath变量时不起作用,但在我使用相同的路径手动设置变量时起作用 更多信息 使用subprocess.call时,我使用字符串变量指定命令的cwd。当手动定义字符串时,一切都按其应有的方式工作。但是,我想从文本文件中的值加载cwd路径。我也已经确定了那个部分,我正在从文本文件加载正确的值。当cwd=filepath和filepath设置为从文本文件加载的字符串值时,我会得到一个notDirectory

TL;DR

subprocess.call(cwd=filepath)
在我从文本文件设置
filepath
变量时不起作用,但在我使用相同的路径手动设置变量时起作用

更多信息

使用
subprocess.call
时,我使用字符串变量指定命令的
cwd
。当手动定义字符串时,一切都按其应有的方式工作。但是,我想从文本文件中的值加载cwd路径。我也已经确定了那个部分,我正在从文本文件加载正确的值。当
cwd=filepath
filepath
设置为从文本文件加载的字符串值时,我会得到一个
notDirectoryError:[WinError 267]目录名无效。
请记住,如果我手动将变量设置为完全相同的路径,则不会出现此错误。我认为这是某种格式问题,我已经在互联网上玩了几天了,但还没有找到有效的解决方案

完整代码

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()
其他信息,谢谢

最初,我只是从文件中提取字符串,没有pathlib元素。我尝试过只使用pathlib,而不将其转换为字符串。值得一提的是

对于其他上下文,“设置”文件是一行,其中包含一行:

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

通过解析它来提取路径。我已验证路径是否正确提取


感谢您的帮助。谢谢

对于其他有相同问题的人,请在字符串末尾添加.rstring()。它将去除字符串中的任何行尾和其他有时不可见的元素

更新后的行内容如下:
blenderfilepath=str(pathlib.Path(bfp[1]).rstring())


谢谢你的帮助

从文本文件中读取的行将由换行符终止。你需要在使用前把它脱掉。谢谢你的回复。您的评论使我使用了.rstring(),它解决了这个问题。非常感谢!