printbob.py:
import sys
for arg in sys.argv:
print arg
getbob.py
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
Tôi có:
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite]
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo]
WindowsError: [Error 193] %1 is not a valid Win32 application
Tôi làm gì sai ở đây? Tôi chỉ muốn có được đầu ra của một tập lệnh Python khác bên trong một tập lệnh Python khác. Tôi có cần gọi cmd.exe hay tôi chỉ có thể chạy printbob.py và gửi lệnh cho nó không?
Khi được hỏi ngày 22 tháng 5 năm 2011 lúc 4:29May 22, 2011 at 4:29
proc = subprocess.Popen[['python', 'printbob.py', 'arg1 arg2 arg3 arg4'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT]
print proc.communicate[][0]
Mặc dù vậy, phải có một cách tốt hơn để làm điều đó, vì kịch bản cũng nằm trong Python. Tốt hơn là tìm ra cách nào đó để tận dụng điều đó hơn những gì bạn đang làm.
Đã trả lời ngày 22 tháng 5 năm 2011 lúc 4:33May 22, 2011 at 4:33
2
Đây là phương pháp sai.
Bạn nên tái cấu trúc
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite]
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo]
WindowsError: [Error 193] %1 is not a valid Win32 application
0 để nó có thể được nhập bằng các mô -đun Python khác. Phiên bản này có thể được nhập và gọi từ dòng lệnh:#!/usr/bin/env python
import sys
def main[args]:
for arg in args:
print[arg]
if __name__ == '__main__':
main[sys.argv]
Ở đây nó được gọi từ dòng lệnh:
python printbob.py one two three four five
printbob.py
one
two
three
four
five
Bây giờ chúng tôi có thể nhập nó trong
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite]
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo]
WindowsError: [Error 193] %1 is not a valid Win32 application
1:#!/usr/bin/env python
import printbob
printbob.main['arg1 arg2 arg3 arg4'.split[' ']]
Nó đang chạy:
python getbob.py
arg1
arg2
arg3
arg4
Đã trả lời ngày 22 tháng 5 năm 2011 lúc 4:54May 22, 2011 at 4:54
Johnsywebjohnsywebjohnsyweb
Huy hiệu vàng 132K2323 gold badges178 silver badges240 bronze badges
2
Đối số shell [mặc định là sai] chỉ định xem có nên sử dụng shell làm chương trình để thực thi hay không. Nếu shell là đúng, bạn nên truyền args dưới dạng chuỗi chứ không phải là một chuỗi
Chỉ cần bọc tất cả các đối số trong một chuỗi và cho
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite]
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo]
WindowsError: [Error 193] %1 is not a valid Win32 application
2proc = subprocess.Popen["python myScript.py --alpha=arg1 -b arg2 arg3" ,stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True]
print proc.communicate[][0]
Đã trả lời ngày 18 tháng 12 năm 2014 lúc 23:07Dec 18, 2014 at 23:07
1
Hàm
File "C:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite]
File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo]
WindowsError: [Error 193] %1 is not a valid Win32 application
3 đã được thêm vào trong Python 3.5.import subprocess
cmd = subprocess.run[["ls", "-ashl"], capture_output=True]
stdout = cmd.stdout.decode[] # bytes => str
Tham khảo: PEP 324 - PEP đề xuất mô -đun phụ
Đã trả lời ngày 6 tháng 6 năm 2019 lúc 12:32Jun 6, 2019 at 12:32
NiconingniconingNicoNing
2.95610 Huy hiệu bạc20 Huy hiệu Đồng10 silver badges20 bronze badges
0
Trong hướng dẫn này, bạn sẽ thấy cách chạy một kịch bản Python từ một kịch bản Python khác.
Cụ thể hơn, bạn sẽ thấy các bước tới:
- Chạy một tập lệnh python từ một tập lệnh khác
- Gọi một biến cụ thể từ tập lệnh python này sang tập lệnh khác
Nhưng trước khi chúng tôi bắt đầu, đây là một mẫu đơn giản mà bạn có thể sử dụng để chạy một tập lệnh Python từ một tập lệnh khác [đối với các tập lệnh Python được lưu trữ trong cùng một thư mục]:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
0Bước 1: Đặt các tập lệnh Python vào cùng một thư mục
Để bắt đầu, hãy đặt các tập lệnh Python của bạn vào cùng một thư mục.
Ví dụ, hãy để giả sử rằng hai tập lệnh Python [được gọi là Python_1 và Python_2] được lưu trữ trong cùng một thư mục:python_1 and python_2] are stored in the same folder:
Python_1 Python_2
python_2
Mục tiêu cuối cùng là chạy tập lệnh Python_2 từ tập lệnh Python_1.
Bước 2: Thêm cú pháp
Tiếp theo, thêm cú pháp vào từng tập lệnh của bạn.
Chẳng hạn, hãy để thêm cú pháp sau trong python_1 & nbsp; script: script:python_1 script:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
1Where:
- Dòng đầu tiên của ‘Nhập Python_2, trong tập lệnh Python_1, sẽ gọi tập lệnh Python_2 thứ haipython_1 script, would call the second python_2 script
- Dòng thứ hai của mã chỉ đơn giản là in biểu thức của ‘Bạn đang làm gì?
Bây giờ, hãy để thêm cú pháp trong tập lệnh Python_2:python_2 script:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
2Trong trường hợp này, biểu thức của ‘Hello World, sẽ được in khi chạy tập lệnh thứ hai.
Lưu ý rằng trước tiên bạn phải lưu cú pháp được ghi trong tập lệnh Python_2 trước khi gọi nó từ một tập lệnh khác.save the syntax that was captured in the python_2 script before calling it from another script.
Bước 3: Chạy một tập lệnh Python từ một bản khác
Bây giờ bạn sẽ cần chạy tập lệnh từ hộp Python_1 để gọi tập lệnh thứ hai.python_1 box in order to call the second script.
Lưu ý rằng kết quả của tập lệnh Python_2 sẽ được hiển thị đầu tiên và chỉ sau đó kết quả của tập lệnh Python_1 mới được hiển thị:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
3Gọi một biến cụ thể từ tập lệnh python này sang tập lệnh khác
Bây giờ, hãy xem cách gọi một biến cụ thể [mà chúng ta sẽ gọi ‘X,] từ tập lệnh Python_2 vào tập lệnh Python_1.
Trong trường hợp đó, bạn sẽ cần chỉnh sửa cú pháp trong tập lệnh Python_1 thành phần sau:python_1 script to the following:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
4Tiếp theo, gán một giá trị [ví dụ: ‘Xin chào thế giới] cho biến‘ X, trong tập lệnh Python_2:python_2 script:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
5Don Tiết quên lưu các thay đổi trong tập lệnh Python_2.python_2 script.
Cuối cùng, hãy chạy cú pháp từ tập lệnh Python_1 và biểu thức ‘Hello World sẽ được in:python_1 script, and the ‘hello world’ expression would be printed:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
6Tương tác của các biến từ hai tập lệnh
Trong phần cuối cùng của hướng dẫn này, bạn sẽ thấy các biến từ hai tập lệnh có thể tương tác như thế nào.
Ví dụ, hãy để giả sử rằng tập lệnh python_1 có biến là y = 2, trong khi tập lệnh python_2 có biến là x = 5. Mục tiêu là tổng hợp hai biến đó và hiển thị kết quả.
Đầu tiên, hãy sửa đổi cú pháp trong tập lệnh Python_1 thành phần sau:python_1 script to the following:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
7Sau đó, thay đổi cú pháp trong tập lệnh Python_2 thành:python_2 script to:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
8Như trước đây, don không quên lưu các thay đổi trong tập lệnh Python_2.python_2 script.
Cuối cùng, hãy chạy cú pháp từ tập lệnh Python_1 và bạn sẽ nhận được ‘7, đó thực sự là tổng của hai biến:python_1 script, and you’ll get ‘7’ which is indeed the sum of the two variables:
import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3 [commands are seperated by spaces]
print subprocess.Popen[['printbob.py', 'arg1 arg2 arg3 arg4']].wait[]
x = raw_input['done']
9