文件路径中的 os.system() 空格 - 无法识别“C:\Program”

os.system() spaces in file path - 'C:\Program' is not recognized

提问人:Tomás Barbosa 提问时间:11/3/2023 更新时间:11/3/2023 访问量:72

问:

我正在尝试使用以下代码将xlsb文件转换为xlsx:

import os

EXCELCNV_PATH = "\"C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCELCNV.EXE\""
FILE_PATH = "\"C:\\Users\\Tomas\\Desktop\\fix_excelcnv\\Book1"
cmd = "cmd /c " + EXCELCNV_PATH 
cmd = cmd + " " + FILE_PATH + ".xlsb\" " + FILE_PATH +".xlsx\""
print(cmd)

os.system(cmd)

我收到此错误消息“'C:\Program'未被识别为内部或外部命令, 可操作的程序或批处理文件。我在路径周围使用引号,我认为这可以解决问题,但显然不是,值得注意的是,如果我只使用具有相同结构的第一个路径:

import os

EXCELCNV_PATH = "\"C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE\""
FILE_PATH = "\"C:\\Users\\Tomas\\Desktop\\fix_excelcnv\\Book1"
cmd = "cmd /c " + EXCELCNV_PATH 
#cmd = cmd + " " + FILE_PATH + ".xlsb\" " + FILE_PATH +".xlsx\""
print(cmd)

os.system(cmd)

excel 打开得很好,所以我认为问题不在于路径的编写方式,但这里肯定发生了一些奇怪的事情,有人能理解我如何做到这一点吗?

我已经尝试使用进程模块而不是 os.system,我尝试使用不同类型的引号,我尝试打印 cmd 并自己在终端上运行命令(它起作用了,这让它现在不起作用看起来更奇怪......我已经尝试了我能想到的一切......

python excel 路径 转义 os.system

评论

0赞 John Gordon 11/3/2023
如果要在字符串中包含文字双引号,则使用单引号作为最外层的分隔符会更简洁,这样就不必转义双引号。'"Hello"'
0赞 John Gordon 11/3/2023
而且使用原始字符串也更干净,因此您不必转义反斜杠。
2赞 picobit 11/3/2023
如果您开始使用而不是尝试手动构建路径字符串,您将为自己节省很多痛苦和痛苦。docs.python.org/3/library/pathlib.htmlpathlib
0赞 Tomás Barbosa 11/3/2023
我尝试过单引号,但它对我没有影响,你试过吗,它对你有用吗?

答:

1赞 Student Seng 11/3/2023 #1

您的脚本将生成以下命令

cmd /c "C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsb" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsx"

即使在终端中手动运行,也应该给您相同的错误(我的猜测是您在没有前面的命令的情况下运行了命令,这就是您认为它有效的原因)。cmd /c

主要问题在于 ,这在提供的帮助中得到了解释:cmd /ccmd /?

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

1.  If all of the following conditions are met, then quote characters
    on the command line are preserved:

    - no /S switch
    - exactly two quote characters
    - no special characters between the two quote characters,
      where special is one of: &<>()@^|
    - there are one or more whitespace characters between the
      two quote characters
    - the string between the two quote characters is the name
      of an executable file.

2.  Otherwise, old behavior is to see if the first character is
    a quote character and if so, strip the leading character and
    remove the last quote character on the command line, preserving
    any text after the last quote character.

因此,如果不需要它,您可以将其删除,或者将整个命令(3 个字符串的序列)包含在一个字符串本身中:cmd /c

cmd /c ""C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsb" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsx""

如果它看起来很奇怪,请不要担心,cmd 会正确解析它。

此外,您的命令似乎没有按照您的要求执行 - “将 xlsb 文件转换为 xlsx”。
没有很好的文档记录,但似乎您缺少一个标志,因此可以尝试以下命令:
excelcnv.exe-oice

cmd /c ""C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE" -oice "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsb" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsx""

要在 Python 中构建此命令,最好按照@picobit建议执行,特别是对于更复杂的脚本,但在这里我将提供与您的代码类似的代码,并添加 和 字符串以提高可读性:pathlibfr

import os

EXCELCNV_PATH = r'"C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE"'
FILE_PATH_STEM = r'C:\Users\Tomas\Desktop\fix_excelcnv\Book1'
XLSB_FILE_PATH = f'"{FILE_PATH_STEM}.xlsb"'
XLSX_FILE_PATH = f'"{FILE_PATH_STEM}.xlsx"'
convert_cmd = f'{EXCELCNV_PATH} -oice {XLSB_FILE_PATH} {XLSX_FILE_PATH}'
cmd = f'cmd /c "{convert_cmd}"'
print(cmd)

os.system(cmd)