Python Forum
Printing a raw string with a folder separator at the end, duplicates the separator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing a raw string with a folder separator at the end, duplicates the separator
#1
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?
Reply
#2
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\
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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.
Reply
#4
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
Reply
#5
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/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 480 Feb-02-2024, 06:06 PM
Last Post: tester_V
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 524 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  remove partial duplicates from csv ledgreve 0 784 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,468 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Problem : Count the number of Duplicates NeedHelpPython 3 4,361 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  printing a string in reverse Skaperen 2 1,540 Nov-20-2021, 05:08 AM
Last Post: ghoul
  printing an string instead of a expression Underscore 2 1,810 Oct-11-2021, 03:10 PM
Last Post: deanhystad
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,465 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Removal of duplicates teebee891 1 1,787 Feb-01-2021, 12:06 PM
Last Post: jefsummers
  Displaying duplicates in dictionary lokesh 2 1,977 Oct-15-2020, 08:07 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020