Does Python convert str to path type?

I am trying to interact with some existing code that saves the configuration and expects a file path that is of type 'path.path'. The code expects the file path to be returned from the pygtk browser window (via another function). I want to call the save_config function elsewhere in my code with a filepath based on different inputs built from string items.

When I try to run the code, I can build the file path correctly, but it is a string type and the save function expects a path.path type.

Is there a way to convert a string to a path type? I tried searching, but could only find reverse case (string path). I also tried using os.path.join()

but also returns a string.

edit: It's python 2.7, if that matters.

+3


source to share


2 answers


If path.path

represents a type, perhaps you can instantiate that type with something like:



string_path = "/path/to/some/file"
the_path = path.path(string_path)
save_config(the_path()

      

+1


source


Maybe this answer worked for python 2.7, if you're on Python 3 I like:



import os

p = "my/path/to/file.py"
os.path.normpath(p)
'my\\path\\to\\file.py'

      

+1


source







All Articles