簡體   English   中英

Python子進程命令行錯誤

[英]Python subprocess command line error

我感到很困惑。 所以我試圖使用我的程序program10.py ,它使用另一個程序,例如other_program

所以我可能會這樣運行:

python3 program10.py other_program

other_program接受一個int參數這是我的program10.py代碼:

import time
import subprocess
n = 2**10
myList = []
while n <= 2**15:
    before = time.process_time()
    subprocess.call(n)
    after = time.process_time()
    print(n, after-before)
    myList.append(after-before)
    n *= 2

print(myList)

當然,我得到了一個大錯誤:

Traceback (most recent call last):
  File "program10.py", line 7, in <module>
    subprocess.call(n)
  File "/usr/lib/python3.5/subprocess.py", line 557, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1440, in _execute_child
    args = list(args)
TypeError: 'int' object is not iterable

我毫不懷疑我在使用subprocess.call是完全錯誤的,因為我不理解它,其他任何SO問題或Python文檔都沒有幫助我。 如果有人可以告訴我它與我的程序之間的關系,那將意味着很多。

“ TypeError:'int'對象不可迭代”行告訴您該函數需要可迭代的,而不是整數。 嘗試傳遞一個字符串或字符串列表。

您應該在subprocess.call()要運行的程序的名稱。 該錯誤表明int不可迭代。 可迭代對象是字符串,列表或元組之類的對象。 它們是一個“包含”多個項目的對象,並且可以一次返回一個項目。 subprocess.call()通常需要一個包含命令和要運行的任何參數的列表。 例如:

subprocess.call(['other_program', str(n)])

但這只會返回程序的返回碼。 如果需要程序創建的任何輸出,則需要使用其他函數,例如subprocess.check_output()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM