-3

Can anyone explain how to work with paths with python on windows.

the path is given as parameter

path = 'C:\Data\Projects\IHateWindows\DEV_Main\Product\ACSF\Dev\DEV\force.com\src\aura'

Im trying to get the content of the folder but is not a valid path is reading the path as:

'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\x0corce.com\\src\x07ura'

trying some solutions...

for f in listdir(path.replace("\\", "\\\\")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\\\Data\\\\Projects\\\\Prd_Development\\\\DEV_Main\\\\Product\\\\ACSF\\\\Dev\\\\DEV\x0corce.com\\\\src\x07ura'


for f in listdir(path.replace("\\", "/")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:/Data/Projects/Prd_Development/DEV_Main/Product/ACSF/Dev/DEV\x0corce.com/src\x07ura'

EDIT: Solution

path = path.replace("\a", "\\a").replace("\f", "\\f")

https://docs.python.org/2.0/ref/strings.html

Alezis
  • 1,182
  • 3
  • 13
  • 25
  • Try use \\ or / when assign value to `path`. – atline Oct 08 '18 at 09:08
  • 1
    Note: your edit shows you are trying to modify the path *inside Python*. That is where you go wrong. You must **enter it correctly** -- with double backslashes or single forward slashes. Trying to use `replace` inside Python is 'too late'. (So it **is** a duplicate after all, and the answer(s) there should help you further.) – Jongware Oct 08 '18 at 09:31
  • As I said tthe path s received as parameter, It comes how it comes.... – Alezis Oct 08 '18 at 09:33
  • @usr2564301 You can modify the path in Pytohn. The only thing is that you need to know what you have at the beginning clearly, and not to get messed up with the str representation. – Mathieu Oct 08 '18 at 09:34
  • 1
    @Mathieu: no, as soon as the path is entered **with the wrong string representation** -- single backslashes instead of doubles -- *you cannot know what the original string was*. You can't blindly replace every `\f` with `\\f`, for example. – Jongware Oct 08 '18 at 09:54
  • @usr2564301 Agree. – Mathieu Oct 08 '18 at 10:29

1 Answers1

-1

A single Backslash is a escape character in Python. Therefore you might need to use double Backslashes or a forward slash:

path = 'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\\force.com\\src\\aura'

or

path = 'C:/Data/Projects/IHateWindows/DEV_Main/Product/ACSF/Dev/DEV/force.com/src/aura'
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47