-1

I have to take in a file path that looks like this:

'C:/Users/xxx/Desktop/test_folder'

It gets stored into a variable as a string so:

path_intake = 'C:/Users/xxx/Desktop/test_folder'

I want to assign that path to my

p = Path(path_intake)

But, When p takes in path_intake it changes the path to:

'C:\Users\xxx\Desktop\test_folder'

Which is not what I want since .rglob can only read the path like this:

p = Path(C:/Users/xxx/Desktop/test_folder)

How to obtain this path by taking in the first path?

taras
  • 6,566
  • 10
  • 39
  • 50
Hugo
  • 49
  • 4

1 Answers1

0

The value

C:/Users/xxx/Desktop/test_folder

is not a canonical Windows path string. As everyone knows, Windows uses backslashes. So if you supply /, pathlib turns the path into a canonical path string for your platform, which is

C:\Users\xxx\Desktop\test_folder

But the two Path objects are identical, as you will quickly see if you do this:

>>> p = pathlib.Path(r"C:\Users\xxx\Desktop\test_folder")     
>>> p2 = pathlib.Path(r"C:/Users/xxx/Desktop/test_folder")     
>>> p == p2     
True

You are not correct when you say that ".rglob can only read a path like this: C:/Users/xxx/Desktop/test_folder". To demonstrate that, do this:

>>> list(p.rglob("*.txt")) == list(p2.rglob("*.txt"))
True

The Path objects are identical and you can call .rglob() on either one and get the expected result.

BoarGules
  • 16,440
  • 2
  • 27
  • 44