简体   繁体   中英

Python working with os.system

I have the code below:

def display_keyboard(*_):
    os.system(r'start " " C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe')

The problem is that when this code runs, I get the following error:

The system cannot find the file C:\Program.

I have an issue working out withespaces so that it search for the entire file

C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe

Using subprocess.Popen() or subprocess.run() is almost always a better choice than os.system() .

import subprocess

def display_keyboard(*args):
    return subprocess.Popen(r'C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe')

Some reasons include:

  • No need to worry about escaping arguments;
  • Much more control over the child process, for example over stdout, stderr and stdin streams;
  • wait() and terminate() methods, etc.

you should instead use

os.startfile("C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\TabTip.exe")

I think

When running commands from the commandline you must either escape whitespace characters or encompass them in quotes (I can't remember if you have to use double quotes or not in windows, it's been a while). This is because spaces are treated as separators between arguments. Try

os.system(r'start " " "C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\TabTip.exe"')

在路径周围尝试引号:

os.system('"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe"')

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