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.
To convert a string to path, we can use the built-in java. nio. file. Paths class get() static method in Java.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With