P
P
pythonMyLife2021-06-21 13:42:44
Python
pythonMyLife, 2021-06-21 13:42:44

Why is os.system not working?

I want to open a specific xls file using Microsoft Excel, command

"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" "D:\Misha\Языки\Python\Excel\basics_0.xls"
in the console it does, but os.system returns an error when it is executed:
>>> from os import system
>>> system (r'"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" "D:\Misha\Языки\Python\Excel\basics_0.xls"')
1

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-06-21
@pythonMyLife

It is necessary either to wrap each file name with a space in quotes, and escape the quotes, respectively, with a backslash, or try to use os.startfile
In general, it is recommended to run this business in a subprocess, something like this:

import subprocess
my_file = "c:/path/to/my_file.xls"
proc = subprocess.Popen(f"c:/program files/…/excel.exe {my_file}", shell=False)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question