Convert a str to path type?

2024/11/19 23:33:28

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.

Answer

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

https://en.xdnf.cn/q/26382.html

Related Q&A

Partially transparent scatter plot, but with a solid color bar

In Python, with Matplotlib, how to simply do a scatter plot with transparency (alpha < 1), but with a color bar that represents their color value, but has alpha = 1?Here is what one gets, with from…

Semaphores on Python

Ive started programming in Python a few weeks ago and was trying to use Semaphores to synchronize two simple threads, for learning purposes. Here is what Ive got: import threading sem = threading.Semap…

Overload () operator in Python

I am trying to learn currying in Python for my class and I have to overload the () operator for it. However, I do not understand how can I can go about overloading the () operator. Can you explain the …

Python pandas apply function if a column value is not NULL

I have a dataframe (in Python 2.7, pandas 0.15.0):df=A B C 0 NaN 11 NaN 1 two NaN [foo, bar] 2 three 33 NaNI want to apply a simple function for ro…

python pip trouble installing from requirements.txt

Ive had great luck with pip in the past, but working at installing some stuff in a venv on is giving me some headaches. I keep getting errors likeNo distributions at all found for somepackage Storing d…

Using py.test with coverage doesnt include imports

For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didnt help.Were using py.test as a test runner. However, we are unable to add the imports and other…

Determine if a Python class is an Abstract Base Class or Concrete

My Python application contains many abstract classes and implementations. For example:import abc import datetimeclass MessageDisplay(object):__metaclass__ = abc.ABCMeta@abc.abstractpropertydef display(…

Pandas printing ALL dtypes

This seems like a very simple problem, however its driving me round the bend. Im sure it should be solved by RTFM, but Ive looked at the options and I can see the one to fix it.I just want to print the…

Which Python API should be used with Mongo DB and Django

I have been going back and forth over which Python API to use when interacting with Mongo. I did a quick survey of the landscape and identified three leading candidates.PyMongo MongoEngine MingIf you w…

Save a list to a .txt file

Is there a function in python that allows us to save a list in a txt file and keep its format?If I have the list:values = [1,2,3]can I save it to a file that contains:[1,2,3]So far I print parts of th…