MrSanderpagen
MrSanderpagen

Reputation: 11

os.startfile() path in python with numbers

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.

Upvotes: 1

Views: 12002

Answers (1)

Felix
Felix

Reputation: 6359

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.

Upvotes: 3

Related Questions