Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a str to path type?

I am trying to interface with some existing code that saves a configuration, and expects a file path that is of type path.path. The code is expecting that the file path is returned from a pygtk browser window (via another function). I want to call the save_config function elsewhere in my code with a file path based on different inputs, constructed from string elements.

When I try to run the code, I am able to construct 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've tried searching, but could only find the reverse case (path to string). I also tried using os.path.join(), but that returns a string as well.

Edit: This is python 2.7, if that makes a difference.

like image 626
realityinabox Avatar asked Sep 30 '14 15:09

realityinabox


People also ask

How do you turn a string into a path?

To convert a string to path, we can use the built-in java. nio. file. Paths class get() static method in Java.

What is path type in Python?

path ? Sounds like a class that is defined by the code you are using; it's not a standard type or class. Prior to Python 3.4, there was no standard for paths; functions operating on paths just used the string representation. Python 3.4 introduced the pathlib module, which provides proper objects to represent paths.

How do I get the path of a text file in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.


1 Answers

Since python 3.4:

from pathlib import Path str_path = "my_path" path = Path(str_path) 

https://docs.python.org/3/library/pathlib.html#module-pathlib

like image 100
Ray Avatar answered Sep 21 '22 16:09

Ray