This article summarizes precautions and remedies when dealing with character strings containing "" backslashes, such as when setting a path in a Windows environment. (On Mac, the path delimiter is a "/" slash, so you don't have to worry about it when setting the path.)
For example, when you specify the folder path in the Windows environment as shown below, it may be treated differently from the expected character string.
python
dir_name = 'C:\testDir'
print(dir_name)
#Assumed character string> C:\testDir
#The character string that is actually displayed> C: estDir
The reason this happens is that the windows path delimiter ** "" backslash is ** used in Python for a process called "escape sequences".
What kind of processing is "escape sequence"? For example, when you want to start a new line in a character string.
python
txt = 'Line break → ← line break here'
# print(txt)I want to display as follows
#Line break here →
#← Line break
Even if you press enter just because you want to insert a line break, a line break will occur on the code and an error will occur.
python
txt = 'Line break here →
← Line break'
# SyntaxError: EOL while scanning string literal
Therefore, the method for representing ** special characters such as "line feed" is "escape sequence" **.
The character that means a line break is represented by'\ n
', which is a combination of the backslash'\
'and'n
'.
python
txt = 'Line break here →\n ← line break'
print(txt)
#↓ Displayed character string
#Line break here →
#← Line break
In the example of setting the first path, the \ t
part of dir_name ='C: \ testDir'
is treated as the character " TAB
".
It was recognized as 'C: [TAB] estDir'
by Python, and it was an unnaturally empty character string.
Therefore, there is a way to treat "" as a character string "" backslash as it is instead of "" for escape sequences.
At the beginning of the string, add the acronym "r" for "raw" which means "as is". It is called a raw string, and it will be treated as it is without escaping.
python
#If r is added, it will be treated as a string as it is and will not be escaped.
r'C:\testDir'
#However, at the end, "\Cannot be supported if it contains ""
r'C:\testDir\'
# SyntaxError: EOL while scanning string literal
Since "" is also a special character, it can be expressed by an escape sequence as "this is a backslash".
If you write two backslashes "\\
", they will be treated as backslashes.
python
#Two backslashes "\\Write to escape the backslash itself
'C:\\testDir'
I think this method is the best for the path. Since the delimiter character of the path of Linux and Mac is a "/" slash, if you are conscious of unifying it with "/", you can prevent problems when you change the environment.
Because Windows can also support with the delimiter "/" slash Rewrite the "" backslash with a "/" slash to deal with it.
python
# 「\"Backslash"/Rewrite to slash
'C:/testDir'
As a precaution when rewriting backslashes to slashes When using the function to get the path, the path delimiter is "" backslash.
The following is running on'C: \ testDir'.
python
import os
#Get current directory(Working folder)
current_dir = os.getcwd()
print(current_dir)
# C:\testDir 「\Get with a backslash
If you use this acquired path and set the subsequent paths yourself, you need to be careful not to mix "/" and "".
python
import os
#Get current directory
current_dir = os.getcwd()
#Set a folder for images "/Write the continuation path with
image_dir = f'{current_dir}/image'
print(image_dir)
# C:\testDir/image 「\」「/"Mixed
In the python function, it works even if it is mixed, but when passing the path to another system or manipulating the path as a string, an error occurs. Example) When setting the download folder of Chrome in Selenium
Therefore, as a workaround, replace "" with "/" when getting the path, and then get it.
python
import os
#When getting the current directory, set the delimiter to "/Replaced with
current_dir = os.getcwd().replace(os.sep,'/')
print(current_dir)
# C:/testDir
Thank you for visiting. There are various ways to deal with the "" backslash, but let's set rules on site or by ourselves and unify them so that we can develop without problems.
Use escape sequences Raw string that ignores (disables) escape sequences in Python Get Separator
Recommended Posts