"r" in the with open(r'sampleDonkey.txt', 'w') as file: stands for? | Python Language Forum
H
Heena Goyal Posted on 29/11/2023
Couldn't find what this "r" in the below syntax in the response submitted by you, is for, anywhere? Why do we use this?

with open(r'sampleDonkey.txt', 'w') as file:

Y
Yogesh Chawla Replied on 29/11/2023

r is placed before filename to prevent the characters in filename string to be treated as special character. 
For example, if there is \temp in the file address, then \t is treated as the tab character and error is 
raised of invalid address. The r makes the string raw, that is, it tells that the string is without any special characters. 
The r can be ignored if the file is in same directory and address is not being placed. 
 
# Open function to open the file "MyFile1.txt" (same directory) in read mode and store its reference in the variable file1  
file1 = open("MyFile.txt", "r") 
 
#and "MyFile2.txt" in D:\Text in file2 
file2 = open(r"D:\Text\MyFile2.txt", "r")
 
We can ignore starting r in our script also since the file is present in same location as the script