尝试从python运行shell命令时没有输出

2024-06-01 18:49:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我从未创建过PowerShell脚本或类似的东西,因此我尝试从Python运行该命令,因为我认为我所要做的就是使用os.popen()命令调用它

我有5000多个文件夹,所有文件夹都包含图像,我将使用从github下载的脚本从中提取关键点

当我试着运行Python脚本时,什么都没有显示。有一个窗口包含图像,其中包含应该在我运行命令时显示的关键点,但什么也没有显示

我在PowerShell中的一个文件夹上尝试了该命令,它工作得非常好

这是我的剧本:

import os
import sys
import time

os.chdir(
    r"C:\Users\Adam\Downloads\openpose-1.7.0-binaries-win64-cpu-python3.7-flir-3d\openpose"
)
for root, dirs, files in os.walk(
    r"C:\Users\Adam\Downloads\LIP_MPV_256_192\MPV_256_192\all\all\images\train"
):
    for d in dirs:
        print("got here")
        os.popen(
            "bin\\OpenPoseDemo.exe --image_dir"
            + " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\train\\"
            + d
            + "--write_json"
            + " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\pose_coco\\train\\"
            + d
        )
        time.sleep(5)

Tags: import命令脚本文件夹osdownloadstrainall
1条回答
网友
1楼 · 发布于 2024-06-01 18:49:30

您可以尝试使用Python标准库中的subprocess模块:

import os
import subprocess
import time

os.chdir(
    r"C:\Users\Adam\Downloads\openpose-1.7.0-binaries-win64-cpu-python3.7-flir-3d\openpose"
)
for root, dirs, files in os.walk(
    r"C:\Users\Adam\Downloads\LIP_MPV_256_192\MPV_256_192\all\all\images\train"
):
    for d in dirs:
        print("got here")
        command = [
            "bin\\OpenPoseDemo.exe",
            " image_dir",
            " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\train\\" + d,
            " write_json",
            " C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\pose_coco\\train\\"
            + d
        ]
        subprocess.run(args=command, shell=False, capture_output=True)
        time.sleep(5)

相关问题 更多 >