[英]Python accept predefined input along with any input inside of a user input and save as variable
所以我有一個類似“終端”的程序,用 python 編寫,在這個程序中,我需要接受“mkdir”和另一個輸入,並將 mkdir 之后的輸入保存為變量。 在執行 python 程序時,它會起作用 sys.argv 的工作原理,但這必須在程序內部工作,我不知道如何使它工作。 另外,對於我在標題中說“輸入”的次數感到抱歉,我不知道如何問這個問題。
user = 'user'
def cmd1():
cmd = input(user + '#')
while True:
if cmd == 'mkdir ' + sys.argv[1]: #trying to accept second input here
print('sys.argv[1]')
break
else:
print('Input not valid')
break
看起來您正在嘗試標記您的cmd
。 也就是說,您想按空格分割輸入的各個部分。
cmd = input(user + '#')
parts = cmd.split()
# get the first part as the instruction
instruction = parts[0]
# get everything else as the args
args = parts[1:]
您只是想接受在 mkdir 之后輸入的 arguments 嗎? 如果是這樣,您可以用空格分割字符串並在 mkdir 之后獲取任何單詞。
cmd = input(user + '#')
cmd_split = cmd.split(' ')
if cmd_split[0] == 'mkdir':
args = cmd_split[1:]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.