m0_64423576 2022-09-06 17:11 采纳率: 0%
浏览 22

关于subprocess.Popen的问题,如何解决

我在尝试用Python写一个批量执行脚本的程序;目前遇到了一个解决不了的问题,因为我Python是半桶水,也找不出问题所在以及怎么解决,所以想上这里提问。

代码是用for循环遍历文件夹内的文件,然后用if筛选出.ma格式的文件

img

执行代码后,只输出了3个文件,正常应该是输出4个文件的

img

下面是代码部分:

import subprocess
import os
from io import StringIO

import_lineEdit = "E:/Maya_Project/scenes/原始文件"
output_lineEdit = "E:/Maya_Project/scenes/输出文件"
melFile = "E:/Maya_Project/scenes/原始文件/CreatSphere.mel"
MAYABATCHPATH = r'C:/Program Files/Autodesk/Maya2019/bin/mayabatch.exe'
for i in os.listdir(import_lineEdit): #用for循环遍历文件夹内所有文件
# print(os.path.abspath(i))
if i.split(".")[1] == "ma": #用if筛选出ma文件
mapath = i
outputfile = output_lineEdit+"/"+os.path.basename(mapath)
#print(self.output_lineEdit.text()+"\"+os.path.basename(mapath)) #输出文件路径 E:\Maya_Project\scenes\输出文件\test.ma
#fixmelFile = melFile.os.path.splitext(melFile)[0] + "_batchtool_willdelete" + os.path.splitext(melFile)[1]
#print(melFile.split(".")[0] + "_batchtool_willdelete." + melFile.split(".")[1])
fixfile = open(melFile.split(".")[0] + "_batchtool_willdelete." + melFile.split(".")[1],"w") #打开或创建_batchtool_willdelete.mel文件
savefile = f'\nfile -rename "{outputfile}" ; '
savefiletype = '\nfile -save -type "mayaAscii" -options"v=0;" ; ////' #% outputfile
# print(savefile)
# print(f'{outputfile}')
# 把mel脚本写入内存,并且添加另存为ma文件的脚本
filestring = open(melFile) # 打开mel文件
inbuffer = StringIO() # 实例化StingIO
inbuffer.write(filestring.read()) # 把mel文件里的代码写入内存
inbuffer.write(savefile) # 把保存文件的代码(mel)写入内存
inbuffer.write(savefiletype) #把保存文件格式的代码(mel)写入内存
fixfile.write(inbuffer.getvalue()) # 把内存里的代码写入文件中
filestring.close() # 关闭fixfile文件
fixmelfile = melFile.split(".")[0] + "_batchtool_willdelete." + melFile.split(".")[1]
# print(fixmelfile)
# 设置cmd命令要运行的参数
# cmd命令示例:"C:**\mayabatch.exe"-batch -file "E:\test.ma" -script "E:\CreatSphere_batchtool_willdelete.mel"
cmd = '"{mayaBatchPath}" -batch -file "{maPath}" -script "{melFile}" "{plugins}"'.format(
mayaBatchPath=MAYABATCHPATH,
melFile=fixmelfile, #mel文件
maPath=import_lineEdit+"\"+mapath, #源maya文件路径
plugins="-noAutoloadPlugins",
)
#用子进程调用cmd命令运行mayabatch.exe
# subprocess.check_call(cmd, shell=True)
p = subprocess.Popen(cmd,
shell=True,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
)
p.wait()

尝试过只用subprocess.Popen执行,但是只会输出最后一个文件

img

还有一个问题是,用subprocess.Popen.wait会导致程序阻塞(因为我有个ui界面的,ui界面会卡死),只用subprocess.Popen的话就不会发生阻塞,但是只会输出最后一个文件。

  • 写回答

2条回答 默认 最新

报告相同问题?

问题事件

  • 修改了问题 9月6日
  • 创建了问题 9月6日

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)