简体   繁体   中英

No output when trying to run a shell command from python

I've never created PowerShell script or anything like that so that's why I'm trying to run the command from Python instead since I thought all I have to do is just called it using the os.popen() command.

I have over 5000 folders all containing images that I'm going to extract keypoints from using a script that I've downloaded from github .

When I tried running my Python script nothing shows up. There's a window containing the image with the keypoints that is supposed to show up when I run the command but nothing shows up.

I have tried the command in PowerShell on one folder and it works perfectly.

Here's my script:

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)

You could try with subprocess module from the Python standard library:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM