[英]Subprocess.call() cwd cannot set from text loaded value
TL; 博士
subprocess.call(cwd=filepath)
在我从文本文件设置filepath
变量时不起作用,但是当我使用相同的路径手动设置它时起作用。
更多信息
当我使用subprocess.call
时,我使用字符串变量指定命令的cwd
。 当字符串被手动定义时,一切都按照它应该的方式工作。 但是,我想从文本文件中的值加载 cwd 路径。 我也确定了那部分,并且我正在从文本文件中加载正确的值。 当cwd=filepath
和filepath
设置为从文本文件加载的字符串值时,我收到NotADirectoryError: [WinError 267] The directory name is invalid.
请记住,如果我手动将变量设置为完全相同的路径,则不会出现此错误。 我认为这是某种格式问题,我已经玩过它/在互联网上浏览了几天,但还没有找到可行的解决方案。
完整代码
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:/程序文件/Blender Foundation/Blender 2.81/
它被解析以提取路径。 我已验证该路径已正确提取。
任何帮助表示赞赏。 谢谢!
对于遇到同样问题的其他人,请将 .rstring() 添加到字符串的末尾。 它将去除字符串中的任何行结尾和其他有时不可见的元素。
更新后的行内容为: blenderfilepath = str(pathlib.Path(bfp[1]).rstring())
感谢jasonharper的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.