I got stuck when I was working on Pathlib while studying Python, so I will show you the solution. (Mac)
--Pointed out by @shiracamus
--This solution does not work because the path delimiter is different in Windows environment
--Supports Windows by setting re.sub (r'\\','', str (x))
Example) Check the existence of ~/test blank folder/with the following code
test.py
import pathlib
x = input('')
fpath = pathlib.Path(x)
print(fpath.exists())
python blankCharPathlib.py
path >>> ~/test\ blank\ folder
~/test\ blank\ folder False
It doesn't work because the backslash escapes the half-width space. I tried enclosing it in single quotes or double quotes, but it doesn't make sense
python blankCharPathlib.py
path >>> '~/test\ blank\ folder'
'~/test\ blank\ folder' False
python blankCharPathlib.py
path >>> "~/test\ blank\ folder"
"~/test\ blank\ folder" False
Remove the escape backslash. Solution
test2.py
import pathlib
import re
x = input('path >>> ')
fpath = pathlib.Path(re.sub(r'\\ ',' ',str(x)))
print(str(fpath) ,fpath.exists())
python blankCharPathlib.py
path >>> ~/test\ blank\ folder
~/test blank folder True
Recommended Posts