Python,open.system函数-文件名中包含空格的错误

时间:2018-11-05 21:06:26

标签: python

我正在尝试从系统文件夹中打开文件。

import os

Folder = "C:\\Users\\kasan\\Videos"
File = Folder+'\\'+os.listdir(Folder)[0]

print(File) #C:\Users\kasan\Videos\big bang theoryE12S6.mp4
os.startfile(Folder)#open the folder
os.system(File) #should open the mp4 file

但是os.system()向我抛出一个错误。下面是错误。

'C:\Users\kasan\Videos\big' is not recognized as an internal or external command,
operable program or batch file.

而不是阅读“大爆炸理论E12S6.mp4”,而是仅阅读了第一部分。 如何克服这个问题?预先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以将文件名放在双引号中以保留空格:

os.system('"%s"' % File)