簡體   English   中英

如何將輸入傳遞給在另一個 python 腳本中運行的 python 腳本?

[英]how to pass input to python script running in another python script?

在一個 python 文件中,我有這個:

# Some code here 
exec("i=input()\nprint(i)")
# Insert some way to pass input to code above

當我運行它時,整個腳本會暫停並等待用戶在控制台中輸入輸入。 有沒有一種方法可以在沒有人工交互的情況下自動傳遞輸入。 換句話說,我正在嘗試將輸入傳遞給嵌入在另一個 python 腳本中的 python 腳本,而無需人工交互。 這可能嗎?

在使用exec() function 中的任何方法之前,必須記住exec()支持的所有函數。 要查看這個,我們可以使用dir() function。

from math import *
exec("print(dir())")

Output:

['__builtins__', '__cached__', '__doc__', '__file__',
 '__loader__', '__name__', '__package__', '__spec__', 
'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 
'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 
'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial',
 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 
'hypot', 'inf', 'isclose', 'isfinite', 'isinf',
'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 
'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin',
 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

如您所見exec() function 不支持input() 這是有道理的,因為input()方法專門設計用於從用戶那里獲取輸入。

暫無
暫無

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

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