filename has a method called abspath that returns an object with the absolute path. You can cast that to a string.

...
from path import Path
folder_path = Path('/home/munk/folder/')
filename = folder_path.joinpath('foo')
absolute_path = filename.abspath()
print(absolute_path)  #  '/home/munk/folder/foo'
f = open(absolute_path, 'r')

UPDATE 2023-03-24: Actually use abspath + use the up to date path library

Answer from munk on Stack Overflow
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Implement Path property that returns the full path as a string - Ideas - Discussions on Python.org
October 13, 2024 - pathlib already provides multiple methods / properties which return things as a str object the reasoning is that It is kind of odd that you have a ton of properties that give you parts of the path, but no property that gives you the whole path pathlib.PurePath.name for example I propose an Pathobj.fullpath or Pathobj.full_path property be added this should result in the same operation as str(Pathobj) so that Pathobj.resolve().full_path can also work
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ help with getting file path to string
r/learnpython on Reddit: Help with getting file path to string
February 25, 2025 -

Hello all, beginner here. Trying to write a small function that does the following:

  1. Reads the current directory of files

  2. Then allows the user to chose 1 file from the printed list

  3. Stores that chosen file's absolute filepath to a string

Source code below:

import os

filesFound = os.listdir(os.getcwd())

for num, file in enumerate(filesFound): 
        print(f"#{num}: {file}")
        filepath = filesFound[num]
        directory = os.path.dirname(filepath)

choice = int(input ("\nChoose one\n\n"))

print("Chosen: " + filesFound[choice] + "\n")
print("File chosen path is: \n" + os.path.dirname(filesFound[choice]))

Any help/corrections are greatly appreciated.

Discussions

python - Convert a str to path type? - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I convert path like object to a simple string/list element.
Something is odd here because that is not the output of either of those loops; you will get the string path in both cases. And in any case, you should be able to use a Path object anywhere you can use a string path. Can you show an actual example of where this fails? More on reddit.com
๐ŸŒ r/learnpython
3
1
December 13, 2022
Windows path to Python path
I wonder is there a better solution on the issue to convert windows path to python path? Yes; simply use pathlib. from pathlib import Path win_path = r"" path_universal = Path(win_path) print("Your Python Path: ", path_universal) The more you use it, the more you regret not knowing about it earlier. It's an awesome piece of kit in the standard library, probably my favourite. More on reddit.com
๐ŸŒ r/learnpython
4
2
February 2, 2022
python - How can I convert a pathlib.Path object to a string? - Stack Overflow
When I print image_path I get the full path to the image, but when I try to pass it to a function that uses ffmpeg to create a video file, I get: TypeError: Can't convert 'PosixPath' object to str implicitly ยท I suspect this is because the object is Posix and the ffmpeg shell command expects a string. In other cases I also got related error messages like ยท TypeError: 'PosixPath' object does not support indexing ... Save this answer. ... Show activity on this post. Python ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
YouTube
youtube.com โ€บ watch
python path to string - YouTube
Download this code from https://codegive.com Title: Understanding and Working with Python Paths as StringsIntroduction:In Python, working with file paths is ...
Published: December 26, 2023
๐ŸŒ
YouTube
youtube.com โ€บ codetime
python pathlib path to string - YouTube
Download this code from https://codegive.com Title: Python Pathlib Tutorial: Converting Path to StringPython's pathlib module provides a powerful and conveni...
Published: December 25, 2023
Views: 279
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ data science โ€บ path representation in python
Path Representation in Python | Towards Data Science
August 20, 2024 - When it comes to handling file paths in Python, pathlib offers a range of features that go beyond just fixing the shortcomings of strings and the os module.
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Convert Filesystem Path to String in Python: A Step-by-Step Guide - YouTube
In this video, weโ€™ll explore the process of converting filesystem paths to strings in Python, a crucial skill for any developer working with file management....
Published: October 18, 2025
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i convert path like object to a simple string/list element.
r/learnpython on Reddit: How do I convert path like object to a simple string/list element.
December 13, 2022 -

For example, take this list of WindowsPath objects:

for item in paths:
    print(item)

OUTPUT:
WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0001'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0002'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0003')

When I do:

for item in paths:
   print(str(item))

OUTPUT:
WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0001'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0002'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0003')

This is a constant problem I'm having, we use paths as path-like objects, but everywhere in the code we also need them as strings or iterables, but when we cast them we have to slice the string.

Top answer
1 of 2
1
Something is odd here because that is not the output of either of those loops; you will get the string path in both cases. And in any case, you should be able to use a Path object anywhere you can use a string path. Can you show an actual example of where this fails?
2 of 2
1
I'm assuming you mean pathlib.WindowsPath? The string representation of all pathlib paths is the raw filesystem path (as per the documentation ). If paths is a list of pathlib.WindowsPath objects, you shouldn't get the output you posted: from pathlib import WindowsPath paths = [ WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0001'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0002'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0003') ] for item in paths: print(item) C:\Users\ezbie\Programming\tweepina\bd\profile\0001 C:\Users\ezbie\Programming\tweepina\bd\profile\0002 C:\Users\ezbie\Programming\tweepina\bd\profile\0003 The string format you seem to be seeing would only be produced by repr: for item in paths: print(repr(item)) WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0001') WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0002') WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0003') Note that collections typically use repr on their elements when you convert them to a string directly: print(paths) [WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0001'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0002'), WindowsPath('C:/Users/ezbie/Programming/tweepina/bd/profile/0003')] Maybe that's where the confusion lies?
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ pathlib.html
pathlib โ€” Object-oriented filesystem paths
The string representation of a path is the raw filesystem path itself (in native form, e.g. with backslashes under Windows), which you can pass to any function taking a file path as a string:
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Use pathlib in Python | note.nkmk.me
February 9, 2024 - The pathlib module allows you to manipulate paths as objects. You can create a Path object using the pathlib.Path() constructor by specifying a path string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ create-a-file-path-with-variables-in-python
Create a File Path with Variables in Python - GeeksforGeeks
July 23, 2025 - The task is to create a file path using variables in Python. Different methods we can use are string concatenation and os.path.join(), both of which allow us to build file paths dynamically and ensure compatibility across different platforms. For example, if you have a folder named Documents ...
๐ŸŒ
Medium
medium.com โ€บ @guandika8 โ€บ stop-representing-file-paths-in-python-using-strings-8312791f10f
Stop representing file paths in Python using strings! | by Andika Wirawan | Medium
July 28, 2023 - These characters can cause errors or unexpected behavior when used in file paths. For example, if you try to open a file named โ€œmy file.txtโ€ as a string, Python will interpret it as two separate arguments: โ€œmyโ€ and โ€œfile.txtโ€. You should escape the space character with a backslash or use quotes around the string to avoid this problem.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ os.path.html
os.path โ€” Common pathname manipulations
To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as strings, or bytes, or any object implementing the os.PathLike protocol. Unlike a Unix shell, Python does not do any automatic path expansions.
๐ŸŒ
b.telligent
btelligent.com โ€บ en โ€บ blog โ€บ best-practice-working-with-paths-in-python-part-1-2
Best Practice: Working With Paths In Python (Part 1)
August 25, 2026 - The interpreter then recognizes the correct path, believing it were a Linux system to start with. Use escape sequences. path_dir: str = "C:\\Users\sselt\Documents\\blog_demo" What bothers me besides the illegibility of this is that one does not use escape sequences at every character-separator combination, only before the โ€œUโ€ and โ€œbโ€. Use raw strings with โ€œrโ€ as a prefix to indicate that special characters should not be evaluated.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ windows path to python path
r/learnpython on Reddit: Windows path to Python path
February 2, 2022 -

I have been searching the web for a solution without a result.
I made this small script that can be used. Feel free to use it.
I wonder is there a better solution on the issue to convert windows path to python path?

"""
A path, in Windows "C:\Folder" . in Python it is "C:/Folder".
In Python the "\" character can get interpreted as the ESCAPE character.
When an 'r' or 'R' prefix is present, a character following a backslash is
included in the string without change, and all backslashes are left in the string.
"""
def main():
# - YOUR INPUT - IMPORTANT: keep the r in front of path string here.
win_path = r"<TYPE YOUR PATH HERE>"
# Call function to convert from windows to Python path.
path_py = py_path(win_path)
print("Your Python Path: ", path_py)

def py_path(win_path):
python_path = "" # The result of this script.
# Convert to ASCII list
ascii_values_list = []
for character in win_path:
ascii_values_list.append(ord(character))
# Replace all ASCII values for "\" (=92) with value for "/" (=47).
for i in range(0, len(ascii_values_list)):
if ascii_values_list[i] == 92:
ascii_values_list[i] = 47
path_py = "" # Convert ASCII list to string
for val in ascii_values_list:
path_py = path_py + chr(val)

if path_py[-1] != "/": # Add "/" at end of path if needed.
path_py = path_py + "/"
return path_py

if __name__ == "__main__":
main() # Script goes there.

# EOF

๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ why-do-i-have-to-convert-my-path-variable-to-a-string-to-convert-a-relative-path-to-an-absolute-one-using-python-3
Why do I have to convert my path variable to a string to convert a relative path to an absolute one? (Using Python 3.) (Example) | Treehouse Community
November 29, 2017 - Until Treehouse updates their version of Python, you must convert your pathlib objects to a string before passing them to an os.path function, but in writing real code going forward, it's better to just use your pathlib objects directly
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ python-pathlib-path-object-how-to-convert-to-string.html
Python Pathlib path object how to convert to string
In Python's pathlib module, you can convert a Path object to a string using the str() function or by directly using the str operator.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-9077.html
getting a full path string from a pathlib.PurePath object
March 20, 2018 - is the proper way to get the plain string path of a pathlib.PurePath object or pathlib.Path object to pass it to str() and use what that returns? that is all i can find. the documentation (i have the 3.5.2 PDF) only describes the .name attribute fo...