Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'r' in Python commands

Tags:

python

This has probably answered somewhere at sometime but the titles I have seen don't connect so here it goes. I have seen python commands that read os.listdir(r".\bootstrapper"). What is the 'r' doing?

Cheers...

like image 471
m1rjj00 Avatar asked May 14 '26 20:05

m1rjj00


2 Answers

it prevents the Python interpreter from attaching any special meanings to special characters in the string (such as the backslash), and just interpret it as is (i.e., in its "raw" form). This is one way you can "escape" special characters in strings you use.

You'll often see raw strings in path specifications. Let's say the path contains a directory that starts with t, e.g., c:\tests\data.csv, so you'd not want \t to be interpreted as a tab, hence use the r modifier.

like image 72
Levon Avatar answered May 17 '26 08:05

Levon


r marks raw input. This means that the normal escape characters within the string are ignored (like \ )

like image 41
mirk Avatar answered May 17 '26 10:05

mirk