os.startfile() path in python with numbers

眉间皱痕 提交于 2021-02-10 14:40:36

问题


I am working on a little project in python for work. It involves opening a file with the os.startfile()

And there in lies my problem :

the path to the file contains several numbers. And for some reason I don't understand this cause problems in locating the file.

Is there a work around for this?

Because I can't change the names of the directory or the file.


回答1:


The problem is that the \ character has a special meaning in python, e.g. \n is a newline etc.

You can either do:

os.startfile(r"G:\EEGdatabase\6541455.docx")

Or:

os.startfile("G:\\EEGdatabase\\6541455.docx")

To solve the problem.

See https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals for details.



来源:https://stackoverflow.com/questions/43204473/os-startfile-path-in-python-with-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!