Open file when path contain folder which name starts with double underscore

2.7k Views Asked by At

I can't open a file if its path contains a folder which name starts with double underscore. For example:

File = open('C:\user\__foldername\file.txt')

It works if the folder's name starts with only one underscore , but unfortunately I can't rename it.

Is there any solution for this?

1

There are 1 best solutions below

0
MattDMo On BEST ANSWER

The reason the file is not opening is not because of the double underscore, it is because there is an escape character somewhere in the string. Windows paths should be defined using a raw string literal - by putting an r before the opening quote, escaping the back slashes, or by using forward slashes:

File = open(r'C:\user\__foldername\file.txt')
File = open('C:\\user\\__foldername\\file.txt')
File = open('C:/user/__foldername/file.txt')

Just having a double underscore somewhere in the path shouldn't affect anything, it's still a valid path name.