Python Convert Windows File path in a variable

Question:

Given is a variable that contains a windows file path. I have to then go and read this file. The problem here is that the path contains escape characters, and I can’t seem to get rid of it. I checked os.path and pathlib, but all expect the correct text formatting already, which I can’t seem to construct.

For example this. Please note that fPath is given, so I cant prefix it with r for a rawpath.

#this is given, I cant rawpath it with r 
fPath = "P:pythonttemp.txt"

file = open(fPath, "r")
for line in file:
    print (line)

How can I turn fPath via some function or method from:

"P:pythonttemp.txt"

to

"P:/python/t/temp.txt"

I’ve tried also tried .replace(“”,”/”), which doesnt work.

I’m using Python 3.7 for this.

Asked By: RedBoxes

||

Answers:

You can use os.path.abspath() to convert it:

print(os.path.abspath("P:pythonttemp.txt"))

>>> P:/python/t/temp.txt

See the documentation of os.path here.

Answered By: Nordle

if you would like to do replace then do

replace("\","/")

Answered By: tbalaz

You can use Path function from pathlib library.

from pathlib import Path

docs_folder = Path("some_folder/some_folder/")
text_file = docs_folder / "some_file.txt"
f = open(text_file)
Answered By: Adriano Silva

I’ve solved it.

The issues lies with the python interpreter. t and all the others don’t exist as such data, but are interpretations of nonprint characters.

So I got a bit lucky and someone else already faced the same problem and solved it with a hard brute-force method:

http://code.activestate.com/recipes/65211/

I just had to find it.

After that I have a raw string without escaped characters, and just need to run the simple replace() on it to get a workable path.

Answered By: RedBoxes

When using python version >= 3.4, the class Path from module pathlib offers a function called as_posix, which will sort of convert a path to *nix style path. For example, if you were to build Path object via p = pathlib.Path('C:\Windows\SysWOW64\regedit.exe'), asking it for p.as_posix() it would yield C:/Windows/SysWOW64/regedit.exe. So to obtain a complete *nix style path, you’d need to convert the drive letter manually.

Answered By: blurryroots

I came across similar problem with Windows file paths. This is what is working for me:

    import os
    file = input(str().split('\')
    file = '/'.join(file)

This gave me the input from this:

    "D:test.txt"

to this:

    "D:/test.txt"

Basically when trying to work with the Windows path, python tends to replace ” to ”. It goes for every backslash. When working with filepaths, you won’t have double slashes since those are splitting folder names.
This way you can list all folders by order by splitting ” and then rejoining them by .join function with frontslash.

Hopefully this helps!

Answered By: Isoide

Use below function, this will pass most of the cases

def resolve_path(path):
   parent_replace=['t','n','r','f','v','a','b','00','\']
   child_replace=['/t','/n','/r','/f','/v','/a','/b','/000','/']
   for i in range(len(parent_replace)):
      path=path.replace(parent_replace[i],child_replace[i])
   return path
Answered By: Mainak Deb
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.