Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

change forward slash to backward slash in python

for category in categories:
  folder = os.path.join(directory, category)
  print(folder)

The output is:

C:\Users\Sibarani\OneDrive\Desktop\Data/cats
C:\Users\Sibarani\OneDrive\Desktop\Data/dogs

expected result is

C:\Users\Sibarani\OneDrive\Desktop\Data\cats
C:\Users\Sibarani\OneDrive\Desktop\Data\dogs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Although it is normally better to use forward slashes exclusively, this can be easily accomplished with string.replace().

e.g.

for category in categories:
    folder = os.path.join(directory, category).replace("/", "\\")
    print(folder)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading