這裏有一個解決方案。
把你myfile.py文件,並將其更改爲以下:
import sys
def main(arg = None):
# Put all of your original myfile.py code here
# You could also use raw_input for a fallback, if no arg is provided:
if arg is None:
arg = raw_input()
# Keep going with the rest of your script
# if __name__ == "__main__" ensures this code doesn't run on import statements
if __name__ == "__main__":
#arg = sys.argv[1] allows you to run this myfile.py directly 1 time, with the first command line paramater, if you want
if len(sys.argv) > 0:
arg = sys.argv[1]
else:
arg = None
main(arg)
然後創建另一個Python文件,名爲wrapper.py:
#importing myfile here allows you to use it has its own self contained module
import sys, myfile
# this loop loops through the command line params starting at index 1 (index 0 is the name of the script itself)
for arg in sys.argv[1 : ]:
myfile.main(arg)
,然後在命令行中,你可以簡單地輸入:
python wrapper.py dev qa prod
你也可以把上面這行代碼放到你的run.bat文件中,這樣我就可以看看如下:
cd C:\Users\myname\Desktop
python wrapper.py dev qa prod
stop
更改'raw_input()'爲'sys.argv [1]'。 – jordanm
@jordanm我覺得OP要採取從標準輸入,而不是參數輸入。 –
@YevhenKuzmovych不按他的'run.bat'例子。 – jordanm