简体   繁体   中英

Getting input from user then starting a process with subprocess

I want to have a program that takes the input of a user and then tries to open that file/program. I can do this with subprocess.call([file]) but this only works on basic programs such as notepad. I want to also be able to pass agruments to the program if there is any arguments. Such as:

Simple program(What I have acheived/tried)

import subprocess
file = input()
subprocess.call([file])

Complicated program(Tried this code but gives error as no such file found)

import subprocess
file = input("File Name: ") #File = qemu-system-x86_64 -boot order=d F:/arch
subprocess.call([file]) # Tries to start qemu with -boot order=d F:/arch args

So i tried finding answers for this but all I learnt that to pass the arguments to the program you mush have it like this([file, args]). So on the second example when I try to run a program with arguments i get a error of no file found. Also I cannot use the os module specifically os.system() as I have no access to cmd

On Windows, you could use the single string version for the first parameter:

subprocess.call(file)

because the underlying system call uses a full command line. On a Posix system, you must use a list correctly splitted. The shlex module is a handy way for that:

import subprocess
import shlex
file = input("File Name: ") #File = qemu-system-x86_64 -boot order=d F:/arch
subprocess.call(shlex.split(file))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM