[英]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.