Python Forum

Full Version: Printing a raw string with a folder separator at the end, duplicates the separator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
There are two acceptable ways to define a path to a directory/folder, with and without the end separator
path_wo_sep = r"c:\temp"
path_w_sep = r"c:\temp\\"

print(path_wo_sep)
print(path_w_sep)
Output:
c:\temp c:\temp\\
How can one get a following output? Wall
Output:
c:\Temp\
Related: Am I able to print odd number of backslashes using a raw string?

print(r"\\")
Output:
\\
print(r"\\\\")
Output:
\\\\
Is this a matter of any postprocessing?
You could use string slicing, but I don't know if this is what you have in mind.

path_wo_sep = r"c:\temp"
path_w_sep = r"c:\temp\\"

print(path_wo_sep)
print(path_w_sep[:-1])
Output:
c:\temp c:\temp\
Dear rob101, thank you for the suggestion. First, I would have to check, whether slicing makes sense, i.e, Is there a double backslash at the end of the string? The input of the directory may come from the user and it cannot be predected.

I can replace using a regex
print(re.sub(r"(?<=[\\])(\\)", r"", r"dir1\dir2\\dir3\\\dir4\\\\"))
and end up with an expected:
Output:
dir1\dir2\dir3\dir4\
I understand that it is a cosmetic issue. The operating system is going to collapse multiple backslashes into a single one, or ignore the superfluous. The script works anyway.
Or just check the input string

path = re.sub(r"(\\)$", r"", path)
If path has ending backslash, that backslash is going to be stripped off
You can use forward-slashes and Pathlib:

import os
from pathlib import Path


p = Path("C:/Temp")
# the leading / or \\ is not required

print(p.drive)
print(p.name)
print(p.parts)
print("p.is_dir():", p.is_dir())
# does not exist ono my installation, so it's False


# use of env-varbiables
# building Path to User Python installation
# %localappdata%/Programs/Python/PythonXXX

python_path = Path(os.getenv("localappdata"), "Programs\Python\Python310")
print(python_path, python_path.exists())

python_exe = python_path / "python.exe"
print(python_exe, python_exe.exists())
Output:
C: Temp ('C:\\', 'Temp') p.is_dir(): False C:\Users\Andre\AppData\Local\Programs\Python\Python310 True C:\Users\Andre\AppData\Local\Programs\Python\Python310\python.exe True
A better overview is here: https://realpython.com/python-pathlib/
(Nov-28-2022, 09:23 AM)krulah Wrote: [ -> ]How can one get a following output? Wall
Just turn \🧬around,it will work fine.
>>> path_w_sep = "c:/temp/"
>>> path_w_sep.
'c:/temp/'
As mentiom bye DeaD_EyE can use Pathlib
import pathlib

main_dir = r'c:\temp'
sub_dir = 'sub_dir'
fname = 'filename.tsv'
file_path = pathlib.PurePath(main_dir, sub_dir, fname)
print(file_path)
Output:
c:\temp\sub_dir\filename.tsv
If look at it will see that is a PureWindowsPath with / this way.
print() will show it the normal way.
>>> file_path
PureWindowsPath('c:/temp/sub_dir/filename.tsv')
>>> print(file_path)
c:\temp\sub_dir\filename.tsv