Hướng dẫn run python exe with arguments - chạy python exe với các đối số

Giả sử tôi có một tập tin RegressionSystem.exe. Tôi muốn thực thi thực thi này với đối số -config. Dòng lệnh nên giống như:

RegressionSystem.exe -config filename

Tôi đã thử như:

regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])

Nhưng nó không hoạt động.

Hướng dẫn run python exe with arguments - chạy python exe với các đối số

Hỏi ngày 10 tháng 4 năm 2013 lúc 14:40Apr 10, 2013 at 14:40

1

Bạn cũng có thể sử dụng

regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
0 nếu bạn muốn. Ví dụ,

import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)

Sự khác biệt giữa

regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
1 và
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
2 về cơ bản là
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
1 đang chặn trong khi
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
2 không, với
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
2 cung cấp chức năng chung hơn. Thông thường
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
1 là tốt cho hầu hết các mục đích, về cơ bản nó là một hình thức thuận tiện của
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
2. Bạn có thể đọc thêm tại câu hỏi này.

Đã trả lời ngày 10 tháng 4 năm 2013 lúc 15:10Apr 10, 2013 at 15:10

HJweideHjweidehjweide

11.3k9 Huy hiệu vàng42 Huy hiệu bạc49 Huy hiệu đồng9 gold badges42 silver badges49 bronze badges

Đối với bất kỳ ai khác tìm thấy điều này, bây giờ bạn có thể sử dụng

regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
8. Đây là một ví dụ:

import subprocess
subprocess.run(["RegressionSystem.exe", "-config filename"])

Thay vào đó, các đối số cũng có thể được gửi dưới dạng chuỗi, nhưng bạn sẽ cần đặt

regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
9. Các tài liệu chính thức có thể được tìm thấy ở đây.

Gulzar

Phim thương hiệu vàng 19.6K2099 Huy hiệu bạc167 Huy hiệu đồng20 gold badges99 silver badges167 bronze badges

Đã trả lời ngày 10 tháng 4 năm 2019 lúc 20:28Apr 10, 2019 at 20:28

Hướng dẫn run python exe with arguments - chạy python exe với các đối số

Daniel Butlerdaniel ButlerDaniel Butler

2.8032 Huy hiệu vàng20 Huy hiệu bạc35 Huy hiệu Đồng2 gold badges20 silver badges35 bronze badges

os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename")

Nên làm việc.

Đã trả lời ngày 10 tháng 4 năm 2013 lúc 15:00Apr 10, 2013 at 15:00

Nghỉ ngơiRested

1872 Huy hiệu bạc9 Huy hiệu Đồng2 silver badges9 bronze badges

1

Ở đây tôi muốn đưa ra một ví dụ tốt. Sau đây, tôi đã nhận được đối số

import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
0 của chương trình hiện tại sau đó nối chúng vào một mảng là
import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
1. Cuối cùng tôi đã gọi
import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
2 để vượt qua chúng toàn bộ và trực tiếp:

import subprocess
import sys

argProgram = []

if __name__ == "__main__":

    # Get arguments from input
    argCount = len(sys.argv)
    
    # Parse arguments
    for i in range(1, argCount):
        argProgram.append(sys.argv[i])

    # Finally run the prepared command
    subprocess.call(argProgram)

Trong

import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
3 này, tôi phải chạy một ứng dụng thực thi có tên là `bit7z.exe":

python Bit7zt.py Bit7zt.exe -e 1.zip -o extract_folder

Lưu ý: Tôi đã sử dụng câu lệnh

import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
4 vì tôi không cần đối số đầu tiên. I used of
import subprocess
FNULL = open(os.devnull, 'w')    #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
4 statement because i dont need the first argument.

Đã trả lời ngày 28 tháng 4 năm 2021 lúc 11:25Apr 28, 2021 at 11:25

Hướng dẫn run python exe with arguments - chạy python exe với các đối số

Tôi đã không hiểu làm thế nào các lập luận hoạt động. Ví dụ: "-FPS 30" không phải là một mà là hai đối số phải được thông qua như thế này (PY3)

args=[exe,"-fps","30"].

Có lẽ điều này giúp ai đó.

Đã trả lời ngày 1 tháng 10 năm 2021 lúc 13:34Oct 1, 2021 at 13:34