Python windows path to string

As most other Python classes do, the WindowsPath class, from pathlib, implements a non-defaulted «dunder string» method (__str__). It turns out that the string representation returned by that method for that class is exactly the string representing the filesystem path you are looking for. Here an example:

from pathlib import Path

p = Path('E:\\x\\y\\z')
>>> WindowsPath('E:/x/y/z')

p.__str__()
>>> 'E:\\x\\y\\z'

str(p)
>>> 'E:\\x\\y\\z'

The str builtin function actually calls the «dunder string» method under the hood, so the results are exaclty the same. By the way, as you can easily guess calling directly the «dunder string» method avoids a level of indirection by resulting in faster execution time.

Here the results of the tests I’ve done on my laptop:

from timeit import timeit

timeit(lambda:str(p),number=10000000)
>>> 2.3293891000000713

timeit(lambda:p.__str__(),number=10000000)
>>> 1.3876856000000544

Even if calling the __str__ method may appear a bit uglier in the source code, as you saw above, it leads to faster runtimes.

Python Tips: How to Correctly Format Windows Path in a String Literal?

If you’re working on a Python project that involves Windows paths, you may be wondering how to correctly format them in a string literal. It’s not uncommon to encounter errors or unexpected results if the path isn’t formatted correctly, which can be frustrating if you’re trying to get your code to work. Fortunately, there is a solution.

In this article, we’ll provide you with tips and tricks to help you correctly format your Windows paths in a string literal using Python. We’ll cover everything from backslashes and forward slashes to raw strings and Unicode strings, so no matter what type of path you’re working with, you’ll find the information you need to get it right.

Whether you’re a beginner or an experienced Python developer, this article will provide you with valuable insights and strategies to help you avoid common pitfalls and improve the quality of your code. So if you’re struggling with formatting Windows paths in Python, don’t miss out on this essential guide. Read on to discover the techniques you need to know to get your paths right!

How Should I Write A Windows Path In A Python String Literal?
“How Should I Write A Windows Path In A Python String Literal?” ~ bbaz

Introduction

Python is a popular programming language used for various purposes such as web development, data analysis, and artificial intelligence. When working with Python projects that involve Windows paths, it is essential to format them correctly. This article offers tips and tricks to help you format your Windows paths rightly in a string literal using Python. It will help you avoid errors or unexpected results and improve the quality of your code.

Understanding Windows paths

Before we dive into formatting Windows paths, it’s essential to understand what they are. A Windows path is a reference to a file or directory in a Windows operating system. It includes the drive letter, colon, backslash, and slashes that separate directory names. Windows paths can differ depending on the OS version you’re using:

Windows Version Example Path
Windows 10 C:\Users\username\Desktop\example
Windows 8 C:\Users\username\Desktop\example
Windows 7 C:\Users\username\Desktop\example
Windows XP C:\Documents and Settings\username\Desktop\example

Formatting Windows Paths in Python

Using Backslashes in Path Strings

The most common way to represent a Windows path in Python is to use backslashes (\) to separate directories. However, when writing a backslash in a string, it is an escape character. It can lead to incorrect formatting and errors if not used correctly. One way to avoid this is to add an additional backslash before the original backslash. For instance:

“`path = ‘C:\\Users\\username\\Desktop\\example’“`

Using Forward Slashes in Path Strings

Forward slashes (/) are also used to separate directories in Windows paths. While it may seem confusing to use forward slashes on a Windows system, Python can process them as normal characters. This type of formatting is beneficial when working with URLs or paths across multiple platforms.

“`path = ‘C:/Users/username/Desktop/example’“`

Using Raw Strings

Another way to avoid escaping backslashes in path strings is by using raw strings. They allow us to pass string literals without escaping any characters, including escape characters like backslashes. Here’s how you can use it:

“`path =r’C:\Users\username\Desktop\example’“`

Unicode Strings

If your path contains Unicode characters, you should use Unicode strings. Unicode strings begin with the letter u followed by a quote (either single or double). You can use the unicode_escape encoding to encode strings that contain non-ASCII characters.

“`path = u’C:\\Users\\username\\Desktop\\Παράδειγμα’print(path.encode(‘unicode_escape’))“`Output: b’C:\\\\Users\\\\username\\\\Desktop\\\\\\u03a0\\u03b1\\u03c1\\u03ac\\u03b4\\u03b5\\u03b9\\u03b3\\u03bc\\u03b1′

Tips for Proper Windows Path Formatting in Python

Use os.path Module Functions

Python’s os.path module offers several functions for working with paths. It includes functions like os.path.join(), which can join multiple path components into a single path and replace any backslashes or forward slashes as required. This can help you avoid path formatting errors:

“`import ospath = os.path.join(‘C:’, ‘Users’, ‘username’, ‘Desktop’, ‘example’)print(path)# Output: C:\Users\username\Desktop\example“`

Avoid Using Hard-Coded Paths

Avoid hard-coding paths in your code, especially if you’re working with multiple users or systems. Instead, handle them as environment variables or include them in configuration files. This approach makes it easy to update or modify the paths and prevents compatibility issues across various systems.

Use Pathlib Module

The pathlib module is a new addition in Python 3.4 that provides object-oriented filesystem paths. It enables you to perform various operations on paths such as joining, root, stem, and suffix.

“`from pathlib import Pathpath = Path.cwd() / ‘example’print(path)# Output: C:\Users\username\Documents\example“`

Conclusion

In conclusion, properly formatting Windows paths in Python is crucial to avoid annoying errors and ensure the quality of your code. You can use backslashes, forward slashes, raw strings, and Unicode strings depending on your requirements. Additionally, using the os.path and pathlib modules can simplify path manipulation and improve readability. By following these best practices, you’ll be able to create robust and efficient Python scripts that handle Windows paths correctly.

Thank you for taking the time to read about Python tips for correctly formatting Windows path in a string literal! As many programmers are aware, manipulating file paths in Windows can be a bit of a headache. However, with the right knowledge and techniques, this task can be made much easier. In this article, we have covered some important details that will help you correctly format Windows paths in a string literal using Python.

We hope these tips will come in handy as you work with file paths in your Python projects. Remember that correctly formatting file paths is essential to avoid errors and ensure that your code runs smoothly. We encourage you to incorporate these techniques into your coding habits to save yourself time, energy, and frustration.

We are glad to have been able to share some insights with you, and we hope you found this information valuable. Please feel free to browse our blog for more articles on programming topics. If you have any questions or comments, we would love to hear from you. Don’t hesitate to leave us a message or reach out to us on social media. Thanks again for visiting our blog!

People Also Ask about Python Tips: How to Correctly Format Windows Path in a String Literal?

  1. What is a string literal?
  2. A string literal is a sequence of characters enclosed in quotation marks, used to represent text or data in Python.

  3. How do I format a Windows path in a string literal?
  4. To format a Windows path in a string literal, use double backslashes (\\) instead of single backslashes (\) to escape the special characters in the path. For example:

    path = C:\\Users\\Username\\Documents\\file.txt

  5. Can I use forward slashes (/) instead of backslashes (\) in a Windows path?
  6. Yes, you can use forward slashes (/) instead of backslashes (\) in a Windows path. Python will automatically convert the forward slashes to backslashes when necessary.

  7. What is the os.path module in Python?
  8. The os.path module in Python provides functions for working with file and directory paths on various operating systems, including Windows.

  9. Is there a shortcut for formatting Windows paths in Python?
  10. Yes, you can use a raw string literal (preceded by an ‘r’) to avoid having to escape the backslashes in a Windows path. For example:

    path = rC:\Users\Username\Documents\file.txt

When I started learning Python, there was one thing I always had trouble with: dealing with directories and file paths!

I remember the struggle to manipulate paths as strings using the os module. I was constantly looking up error messages related to improper path manipulation.

The os module never felt intuitive and ergonomic to me, but my luck changed when pathlib landed in Python 3.4. It was a breath of fresh air, much easier to use, and felt more Pythonic to me.

The only problem was: finding examples on how to use it was hard; the documentation only covered a few use cases. And yes, Python’s docs are good, but for newcomers, examples are a must.

Even though the docs are much better now, they don’t showcase the module in a problem-solving fashion. That’s why I decided to create this cookbook.

This article is a brain dump of everything I know about pathlib. It’s meant to be a reference rather than a linear guide. Feel free to jump around to sections that are more relevant to you.

In this guide, we’ll go over dozens of use cases such as:

  • how to create (touch) an empty file
  • how to convert a path to string
  • getting the home directory
  • creating new directories, doing it recursively, and dealing with issues when they
  • getting the current working directory
  • get the file extension from a filename
  • get the parent directory of a file or script
  • read and write text or binary files
  • how to delete files
  • how create nested directories
  • how to list all files and folders in a directory
  • how to list all subdirectories recursively
  • how to remove a directory along with its contents

I hope you enjoy!

Table of contents

  • What is pathlib in Python?
  • The anatomy of a pathlib.Path
  • How to convert a path to string
  • How to join a path by adding parts or other paths
  • Working with directories using pathlib
    • How to get the current working directory (cwd) with pathlib
    • How to get the home directory with pathlib
    • How to expand the initial path component with Path.expanduser()
    • How to list all files and directories
    • Using isdir to list only the directories
    • Getting a list of all subdirectories in the current directory recursively
    • How to recursively iterate through all files
    • How to change directories with Python pathlib
    • How to delete directories with pathlib
    • How to remove a directory along with its contents with pathlib
  • Working with files using pathlib
    • How to touch a file and create parent directories
    • How to get the filename from path
    • How to get the file extension from a filename using pathlib
    • How to open a file for reading with pathlib
    • How to read text files with pathlib
    • How to read JSON files from path with pathlib
    • How to write a text file with pathlib
    • How to copy files with pathlib
    • How to delete a file with pathlib
    • How to delete all files in a directory with pathlib
    • How to rename a file using pathlib
    • How to get the parent directory of a file with pathlib
  • Conclusion

What is pathlib in Python?

pathlib is a Python module created to make it easier to work with paths in a file system. This module debuted in Python 3.4 and was proposed by the PEP 428.

Prior to Python 3.4, the os module from the standard library was the go to module to handle paths. os provides several functions that manipulate paths represented as plain Python strings. For example, to join two paths using os, one can use theos.path.join function.

>>> import os
>>> os.path.join('/home/user', 'projects')
'/home/user/projects'

>>> os.path.expanduser('~')
'C:\\Users\\Miguel'

>>> home = os.path.expanduser('~')

>>> os.path.join(home, 'projects')
'C:\\Users\\Miguel\\projects'

Representing paths as strings encourages inexperienced Python developers to perform common path operations using string method. For example, joining paths with + instead of using os.path.join(), which can lead to subtle bugs and make the code hard to reuse across multiple platforms.

Moreover, if you want the path operations to be platform agnostic, you will need multiple calls to various os functions such as os.path.dirname(), os.path.basename(), and others.

In an attempt to fix these issues, Python 3.4 incorporated the pathlib module. It provides a high-level abstraction that works well under POSIX systems, such as Linux as well as Windows. It abstracts way the path’s representation and provides the operations as methods.

The anatomy of a pathlib.Path

To make it easier to understand the basics components of a Path, in this section we’ll their basic components.

Python pathlib Path parts Linux

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/blog/config.tar.gz')

>>> path.drive
'/'

>>> path.root
'/'

>>> path.anchor
'/'

>>> path.parent
PosixPath('/home/miguel/projects/blog')

>>> path.name
'config.tar.gz'

>>> path.stem
'config.tar'

>>> path.suffix
'.gz'

>>> path.suffixes
['.tar', '.gz']

Python pathlib Path parts Windows

>>> from pathlib import Path

>>> path = Path(r'C:/Users/Miguel/projects/blog/config.tar.gz')

>>> path.drive
'C:'

>>> path.root
'/'

>>> path.anchor
'C:/'

>>> path.parent
WindowsPath('C:/Users/Miguel/projects/blog')

>>> path.name
'config.tar.gz'

>>> path.stem
'config.tar'

>>> path.suffix
'.gz'

>>> path.suffixes
['.tar', '.gz']

How to convert a path to string

pathlib implements the magic __str__ method, and we can use it convert a path to string. Having this method implemented means you can get its string representation by passing it to the str constructor, like in the example below.

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/tutorial')

>>> str(path)
'/home/miguel/projects/tutorial'

>>> repr(path)
"PosixPath('/home/miguel/projects/blog/config.tar.gz')"

The example above illustrates a PosixPath, but you can also convert a WindowsPath to string using the same mechanism.

>>> from pathlib import Path

>>> path = Path(r'C:/Users/Miguel/projects/blog/config.tar.gz')

# when we convert a WindowsPath to string, Python adds backslashes
>>> str(path)
'C:\\Users\\Miguel\\projects\\blog\\config.tar.gz'

# whereas repr returns the path with forward slashes as it is represented on Windows
>>> repr(path)
"WindowsPath('C:/Users/Miguel/projects/blog/config.tar.gz')"

How to join a path by adding parts or other paths

One of the things I like the most about pathlib is how easy it is to join two or more paths, or parts. There are three main ways you can do that:

  • you can pass all the individual parts of a path to the constructor
  • use the .joinpath method
  • use the / operator
>>> from pathlib import Path

# pass all the parts to the constructor
>>> Path('.', 'projects', 'python', 'source')
PosixPath('projects/python/source')

# Using the / operator to join another path object
>>> Path('.', 'projects', 'python') / Path('source')
PosixPath('projects/python/source')

# Using the / operator to join another a string
>>> Path('.', 'projects', 'python') / 'source'
PosixPath('projects/python/source')

# Using the joinpath method
>>> Path('.', 'projects', 'python').joinpath('source')
PosixPath('projects/python/source')

On Windows, Path returns a WindowsPath instead, but it works the same way as in Linux.

>>> Path('.', 'projects', 'python', 'source')
WindowsPath('projects/python/source')

>>> Path('.', 'projects', 'python') / Path('source')
WindowsPath('projects/python/source')

>>> Path('.', 'projects', 'python') / 'source'
WindowsPath('projects/python/source')

>>> Path('.', 'projects', 'python').joinpath('source')
WindowsPath('projects/python/source')

Working with directories using pathlib

In this section, we’ll see how we can traverse, or walk, through directories with pathlib. And when it comes to navigating folders, there many things we can do, such as:

  • getting the current working directory
  • getting the home directory
  • expanding the home directory
  • creating new directories, doing it recursively, and dealing with issues when they already exist
  • how create nested directories
  • listing all files and folders in a directory
  • listing only folders in a directory
  • listing only the files in a directory
  • getting the number of files in a directory
  • listing all subdirectories recursively
  • listing all files in a directory and subdirectories recursively
  • recursively listing all files with a given extension or pattern
  • changing current working directories
  • removing an empty directory
  • removing a directory along with its contents

How to get the current working directory (cwd) with pathlib

The pathlib module provides a classmethod Path.cwd() to get the current working directory in Python. It returns a PosixPath instance on Linux, or other Unix systems such as macOS or OpenBSD. Under the hood, Path.cwd() is just a wrapper for the classic os.getcwd().

>>> from pathlib import Path

>>> Path.cwd()
PosixPath('/home/miguel/Desktop/pathlib')

On Windows, it returns a WindowsPath.

>>> from pathlib import Path

>>> Path.cwd()
>>> WindowsPath('C:/Users/Miguel/pathlib')

You can also print it by converting it to string using a f-string, for example.

>>> from pathlib import Path

>>> print(f'This is the current directory: {Path.cwd()}')
This is the current directory: /home/miguel/Desktop/pathlib

PS: If you

How to get the home directory with pathlib

When pathlib arrived in Python 3.4, a Path had no method for navigating to the home directory. This changed on Python 3.5, with the inclusion of the Path.home() method.

In Python 3.4, one has to use os.path.expanduser, which is awkward and unintuitive.

# In python 3.4
>>> import pathlib, os
>>> pathlib.Path(os.path.expanduser("~"))
PosixPath('/home/miguel')

From Python 3.5 onwards, you just call Path.home().

# In Python 3.5+
>>> import pathlib

>>> pathlib.Path.home()
PosixPath('/home/miguel')

Path.home() also works well on Windows.

>>> import pathlib

>>> pathlib.Path.home()
WindowsPath('C:/Users/Miguel')

How to expand the initial path component with Path.expanduser()

In Unix systems, the home directory can be expanded using ~ ( tilde symbol). For example, this allows us to represent full paths like this: /home/miguel/Desktop as just: ~/Desktop/.

>>> from pathlib import Path

>>> path = Path('~/Desktop/')
>>> path.expanduser()
PosixPath('/home/miguel/Desktop')

Despite being more popular on Unix systems, this representation also works on Windows.

>>> path = Path('~/projects')

>>> path.expanduser()
WindowsPath('C:/Users/Miguel/projects')

>>> path.expanduser().exists()
True

What’s the opposite of os.path.expanduser()?

Unfortunately, the pathlib module doesn’t have any method to do the inverse operation. If you want to condense the expanded path back to its shorter version, you need to get the path relative to your home directory using Path.relative_to, and place the ~ in front of it.

>>> from pathlib import Path

>>> path = Path('~/Desktop/')
>>> expanded_path = path.expanduser()
>>> expanded_path
PosixPath('/home/miguel/Desktop')
>>> '~' / expanded_path.relative_to(Path.home())
PosixPath('~/Desktop')

Creating directories with pathlib

A directory is nothing more than a location for storing files and other directories, also called folders. pathlib.Path comes with a method to create new directories named Path.mkdir().

This method takes three arguments:

  • mode: Used to determine the file mode and access flags
  • parents: Similar to the mkdir -p command in Unix systems. Default to False which means it raises errors if there’s the parent is missing, or if the directory is already created. When it’s True, pathlib.mkdir creates the missing parent directories.
  • exist_ok: Defaults to False and raises FileExistsError if the directory being created already exists. When you set it to True, pathlib ignores the error if the last part of the path is not an existing non-directory file.
>>> from pathlib import Path

# lists all files and directories in the current folder
>>> list(Path.cwd().iterdir())
[PosixPath('/home/miguel/path/not_created_yet'),
 PosixPath('/home/miguel/path/reports')]

# create a new path instance
>>> path = Path('new_directory')

# only the path instance has been created, but it doesn't exist on disk yet
>>> path.exists()
False

# create path on disk
>>> path.mkdir()

# now it exsists
>>> path.exists()
True

# indeed, it shows up
>>> list(Path.cwd().iterdir())
[PosixPath('/home/miguel/path/not_created_yet'),
 PosixPath('/home/miguel/path/reports'),
 PosixPath('/home/miguel/path/new_directory')]

Creating a directory that already exists

When you have a directory path and it already exists, Python raises FileExistsError if you call Path.mkdir() on it. In the previous section, we briefly mentioned that this happens because by default the exist_ok argument is set to False.

>>> from pathlib import Path

>>> list(Path.cwd().iterdir())
[PosixPath('/home/miguel/path/not_created_yet'),
 PosixPath('/home/miguel/path/reports'),
 PosixPath('/home/miguel/path/new_directory')]

>>> path = Path('new_directory')

>>> path.exists()
True

>>> path.mkdir()
---------------------------------------------------------------------------
FileExistsError                           Traceback (most recent call last)
<ipython-input-25-4b7d1fa6f6eb> in <module>
----> 1 path.mkdir()

~/.pyenv/versions/3.9.4/lib/python3.9/pathlib.py in mkdir(self, mode, parents, exist_ok)
   1311         try:
-> 1312             self._accessor.mkdir(self, mode)
   1313         except FileNotFoundError:
   1314             if not parents or self.parent == self:

FileExistsError: [Errno 17] File exists: 'new_directory'

To create a folder that already exists, you need to set exist_ok to True. This is useful if you don’t want to check using if‘s or deal with exceptions, for example. Another benefit is that is the directory is not empty, pathlib won’t override it.

>>> path = Path('new_directory')

>>> path.exists()
True

>>> path.mkdir(exist_ok=True)

>>> list(Path.cwd().iterdir())
[PosixPath('/home/miguel/path/not_created_yet'),
 PosixPath('/home/miguel/path/reports'),
 PosixPath('/home/miguel/path/new_directory')]

>>> (path / 'new_file.txt').touch()

>>> list(path.iterdir())
[PosixPath('new_directory/new_file.txt')]

>>> path.mkdir(exist_ok=True)

# the file is still there, pathlib didn't overwrote it
>>> list(path.iterdir())
[PosixPath('new_directory/new_file.txt')]

How to create parent directories recursively if not exists

Sometimes you might want to create not only a single directory but also a parent and a subdirectory in one go.

The good news is that Path.mkdir() can handle situations like this well thanks to its parents argument. When parents is set to True, pathlib.mkdir creates the missing parent directories; this behavior is similar to the mkdir -p command in Unix systems.

>>> from pathlib import Path

>>> path = Path('new_parent_dir/sub_dir')

>>> path.mkdir()
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-35-4b7d1fa6f6eb> in <module>
----> 1 path.mkdir()

~/.pyenv/versions/3.9.4/lib/python3.9/pathlib.py in mkdir(self, mode, parents, exist_ok)
   1311         try:
-> 1312             self._accessor.mkdir(self, mode)
   1313         except FileNotFoundError:
   1314             if not parents or self.parent == self:

FileNotFoundError: [Errno 2] No such file or directory: 'new_parent_dir/sub_dir'

>>> path.mkdir(parents=True)

>>> path.exists()
True

>>> path.parent
PosixPath('new_parent_dir')

>>> path
PosixPath('new_parent_dir/sub_dir')

How to list all files and directories

There are many ways you can list files in a directory with Python’s pathlib. We’ll see each one in this section.

To list all files in a directory, including other directories, you can use the Path.iterdir() method. For performance reasons, it returns a generator that you can either use to iterate over it, or just convert to a list for convenience.

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/pathlib')
>>> list(path.iterdir())
[PosixPath('/home/miguel/projects/pathlib/script.py'),
 PosixPath('/home/miguel/projects/pathlib/README.md'),
 PosixPath('/home/miguel/projects/pathlib/tests'),
 PosixPath('/home/miguel/projects/pathlib/src')]

Using isdir to list only the directories

We’ve seen that iterdir returns a list of Paths. To list only the directories in a folder, you can use the Path.is_dir() method. The example below will get all the folder names inside the directory.

⚠️ WARNING: This example only lists the immediate subdirectories in Python. In the next subsection, we’ll see how to list all subdirectories.

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/pathlib')

>>> [p for p in path.iterdir() if p.is_dir()]
[PosixPath('/home/miguel/projects/pathlib/tests'),
 PosixPath('/home/miguel/projects/pathlib/src')]

Getting a list of all subdirectories in the current directory recursively

In this section, we’ll see how to navigate in directory and subdirectories. This time we’ll use another method from pathlib.Path named glob.

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/pathlib')

>>> [p for p in path.glob('**/*') if p.is_dir()]
[PosixPath('/home/miguel/projects/pathlib/tests'),
 PosixPath('/home/miguel/projects/pathlib/src'),
 PosixPath('/home/miguel/projects/pathlib/src/dir')]

As you see, Path.glob will also print the subdirectory src/dir.

Remembering to pass '**/ to glob() is a bit annoying, but there’s a way to simplify this by using Path.rglob().

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/pathlib')

>>> [p for p in path.rglob('*') if p.is_dir()]
[PosixPath('/home/miguel/projects/pathlib/tests'),
 PosixPath('/home/miguel/projects/pathlib/src'),
 PosixPath('/home/miguel/projects/pathlib/src/dir')]

How to list only the files with is_file

Just as pathlib provides a method to check if a path is a directory, it also provides one to check if a path is a file. This method is called Path.is_file(), and you can use to filter out the directories and print all file names in a folder.

>>> from pathlib import Path
>>> path = Path('/home/miguel/projects/pathlib')

>>> [p for p in path.iterdir() if p.is_file()]
[PosixPath('/home/miguel/projects/pathlib/script.py'),
 PosixPath('/home/miguel/projects/pathlib/README.md')]

⚠️ WARNING: This example only lists the files inside the current directory. In the next subsection, we’ll see how to list all files inside the subdirectories as well.

Another nice use case is using Path.iterdir() to count the number of files inside a folder.

>>> from pathlib import Path
>>> path = Path('/home/miguel/projects/pathlib')

>>> len([p for p in path.iterdir() if p.is_file()])
2

How to recursively iterate through all files

In previous sections, we used Path.rglob() to list all directories recursively, we can do the same for files by filtering the paths using the Path.is_file() method.

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/pathlib')

>>> [p for p in path.rglob('*') if p.is_file()]
[PosixPath('/home/miguel/projects/pathlib/script.py'),
 PosixPath('/home/miguel/projects/pathlib/README.md'),
 PosixPath('/home/miguel/projects/pathlib/tests/test_script.py'),
 PosixPath('/home/miguel/projects/pathlib/src/dir/walk.py')]

How to recursively list all files with a given extension or pattern

In the previous example, we list all files in a directory, but what if we want to filter by extension? For that, pathlib.Path has a method named match(), which returns True if matching is successful, and False otherwise.

In the example below, we list all .py files recursively.

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/pathlib')

>>> [p for p in path.rglob('*') if p.is_file() and p.match('*.py')]
[PosixPath('/home/miguel/projects/pathlib/script.py'),
 PosixPath('/home/miguel/projects/pathlib/tests/test_script.py'),
 PosixPath('/home/miguel/projects/pathlib/src/dir/walk.py')]

We can use the same trick for other kinds of files. For example, we might want to list all images in a directory or subdirectories.

>>> from pathlib import Path
>>> path = Path('/home/miguel/pictures')

>>> [p for p in path.rglob('*')
         if p.match('*.jpeg') or p.match('*.jpg') or p.match('*.png')
]
[PosixPath('/home/miguel/pictures/dog.png'),
 PosixPath('/home/miguel/pictures/london/sunshine.jpg'),
 PosixPath('/home/miguel/pictures/london/building.jpeg')]

We can actually simplify it even further, we can use only Path.glob and Path.rglob to matching. (Thanks to u/laundmo and u/SquareRootsi for pointing out!)

>>> from pathlib import Path

>>> path = Path('/home/miguel/projects/pathlib')

>>> list(path.rglob('*.py'))
[PosixPath('/home/miguel/projects/pathlib/script.py'),
 PosixPath('/home/miguel/projects/pathlib/tests/test_script.py'),
 PosixPath('/home/miguel/projects/pathlib/src/dir/walk.py')]

>>> list(path.glob('*.py'))
[PosixPath('/home/miguel/projects/pathlib/script.py')]

>>> list(path.glob('**/*.py'))
[PosixPath('/home/miguel/projects/pathlib/script.py'),
 PosixPath('/home/miguel/projects/pathlib/tests/test_script.py'),
 PosixPath('/home/miguel/projects/pathlib/src/dir/walk.py')]

How to change directories with Python pathlib

Unfortunately, pathlib has no built-in method to change directories. However, it is possible to combine it with the os.chdir() function, and use it to change the current directory to a different one.

⚠️ WARNING: For versions prior to 3.6, os.chdir only accepts paths as string.

>>> import pathlib

>>> pathlib.Path.cwd()
PosixPath('/home/miguel')

>>> target_dir = '/home'

>>> os.chdir(target_dir)

>>> pathlib.Path.cwd()
PosixPath('/home')

How to delete directories with pathlib

Deleting directories using pathlib depends on if the folder is empty or not. To delete an empty directory, we can use the Path.rmdir()method.

>>> from pathlib import Path

>>> path = Path('new_empty_dir')

>>> path.mkdir()

>>> path.exists()
True

>>> path.rmdir()

>>> path.exists()
False

If we put some file or other directory inside and try to delete, Path.rmdir() raises an error.

>>> from pathlib import Path

>>> path = Path('non_empty_dir')

>>> path.mkdir()

>>> (path / 'file.txt').touch()

>>> path
PosixPath('non_empty_dir')

>>> path.exists()
True

>>> path.rmdir()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-64-00bf20b27a59> in <module>
----> 1 path.rmdir()

~/.pyenv/versions/3.9.4/lib/python3.9/pathlib.py in rmdir(self)
   1350         Remove this directory.  The directory must be empty.
                      ...
-> 1352         self._accessor.rmdir(self)
   1353
   1354     def lstat(self):

OSError: [Errno 39] Directory not empty: 'non_empty_dir'

Now, the question is: how to delete non-empty directories with pathlib?

This is what we’ll see next.

How to remove a directory along with its contents with pathlib

To delete a non-empty directory, we need to remove its contents, everything.

To do that with pathlib, we need to create a function that uses Path.iterdir() to walk or traverse the directory and:

  • if the path is a file, we call Path.unlink()
  • otherwise, we call the function recursively. When there are no more files, that is, when the folder is empty, just call Path.rmdir()

Let’s use the following example of a non empty directory with nested folder and files in it.

$ tree /home/miguel/Desktop/blog/pathlib/sandbox/
/home/miguel/Desktop/blog/pathlib/sandbox/
├── article.txt
└── reports
    ├── another_nested
    │   └── some_file.png
    └── article.txt

2 directories, 3 files

To remove it we can use the following recursive function.

>>> from pathlib import Path

>>> def remove_all(root: Path):
         for path in root.iterdir():
             if path.is_file():
                 print(f'Deleting the file: {path}')
                 path.unlink()
             else:
                 remove_all(path)
         print(f'Deleting the empty dir: {root}')
         root.rmdir()

Then, we invoke it for the root directory, inclusive.

>>> from pathlib import Path

>>> root = Path('/home/miguel/Desktop/blog/pathlib/sandbox')
>>> root
PosixPath('/home/miguel/Desktop/blog/pathlib/sandbox')

>>> root.exists()
True

>>> remove_all(root)
Deleting the file: /home/miguel/Desktop/blog/pathlib/sandbox/reports/another_nested/some_file.png
Deleting the empty dir: /home/miguel/Desktop/blog/pathlib/sandbox/reports/another_nested
Deleting the file: /home/miguel/Desktop/blog/pathlib/sandbox/reports/article.txt
Deleting the empty dir: /home/miguel/Desktop/blog/pathlib/sandbox/reports
Deleting the file: /home/miguel/Desktop/blog/pathlib/sandbox/article.txt
Deleting the empty dir: /home/miguel/Desktop/blog/pathlib/sandbox

>>> root
PosixPath('/home/miguel/Desktop/blog/pathlib/sandbox')

>>> root.exists()
False

I need to be honest, this solution works fine but it’s not the most appropriate one. pathlib is not suitable for these kind of operations.

As suggested by u/Rawing7 from reddit, a better approach is to use shutil.rmtree.

>>> from pathlib import Path

>>> import shutil

>>> root = Path('/home/miguel/Desktop/blog/pathlib/sandbox')

>>> root.exists()
True

>>> shutil.rmtree(root)

>>> root.exists()
False

Working with files

In this section, we’ll use pathlib to perform operations on a file, for example, we’ll see how we can:

  • create new files
  • copy existing files
  • delete files with pathlib
  • read and write files with pathlib

Specifically, we’ll learn how to:

  • create (touch) an empty file
  • touch a file with timestamp
  • touch a new file and create the parent directories if they don’t exist
  • get the file name
  • get the file extension from a filename
  • open a file for reading
  • read a text file
  • read a JSON file
  • read a binary file
  • opening all the files in a folder
  • write a text file
  • write a JSON file
  • write bytes data file
  • copy an existing file to another directory
  • delete a single file
  • delete all files in a directory
  • rename a file by changing its name, or by adding a new extension
  • get the parent directory of a file or script

How to touch (create an empty) a file

pathlib provides a method to create an empty file named Path.touch(). This method is very handy when you need to create a placeholder file if it does not exist.

>>> from pathlib import Path

>>> Path('empty.txt').exists()
False

>>> Path('empty.txt').touch()

>>> Path('empty.txt').exists()
True

Touch a file with timestamp

To create a timestamped empty file, we first need to determine the timestamp format.

One way to do that is to use the time and datetime. First we define a date format, then we use the datetime module to create the datetime object. Then, we use the time.mktime to get back the timestamp.

Once we have the timestamp, we can just use f-strings to build the filename.

>>> import time, datetime

>>> s = '02/03/2021'

>>> d = datetime.datetime.strptime(s, "%d/%m/%Y")

>>> d
datetime.datetime(2021, 3, 2, 0, 0)

>>> d.timetuple()
time.struct_time(tm_year=2021, tm_mon=3, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=61, tm_isdst=-1)

>>> time.mktime(d.timetuple())
1614643200.0

>>> int(time.mktime(d.timetuple()))
1614643200

>>> from pathlib import Path

>>> Path(f'empty_{int(time.mktime(d.timetuple()))}.txt').exists()
False

>>> Path(f'empty_{int(time.mktime(d.timetuple()))}.txt').touch()

>>> Path(f'empty_{int(time.mktime(d.timetuple()))}.txt').exists()
True

>>> str(Path(f'empty_{int(time.mktime(d.timetuple()))}.txt'))
'empty_1614643200.txt'

How to touch a file and create parent directories

Another common problem when creating empty files is to place them in a directory that doesn’t exist yet. The reason is that path.touch() only works if the directory exists. To illustrate that, let’s see an example.

>>> from pathlib import Path

>>> Path('path/not_created_yet/empty.txt')
PosixPath('path/not_created_yet/empty.txt')

>>> Path('path/not_created_yet/empty.txt').exists()
False

>>> Path('path/not_created_yet/empty.txt').touch()
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-24-177d43b041e9> in <module>
----> 1 Path('path/not_created_yet/empty.txt').touch()

~/.pyenv/versions/3.9.4/lib/python3.9/pathlib.py in touch(self, mode, exist_ok)
   1302         if not exist_ok:
   1303             flags |= os.O_EXCL
-> 1304         fd = self._raw_open(flags, mode)
   1305         os.close(fd)
   1306

~/.pyenv/versions/3.9.4/lib/python3.9/pathlib.py in _raw_open(self, flags, mode)
   1114         as os.open() does.
                      ...
-> 1116         return self._accessor.open(self, flags, mode)
   1117
   1118     # Public API

FileNotFoundError: [Errno 2] No such file or directory: 'path/not_created_yet/empty.txt'

If the target directory does not exist, pathlib raises FileNotFoundError. To fix that we need to create the directory first, the simplest way, as described in the «creating directories» section, is to use the Path.mkdir(parents=True, exist_ok=True). This method creates an empty directory including all parent directories.

>>> from pathlib import Path

>>> Path('path/not_created_yet/empty.txt').exists()
False

# let's create the empty folder first
>>> folder = Path('path/not_created_yet/')

# it doesn't exist yet
>>> folder.exists()
False

# create it
>>> folder.mkdir(parents=True, exist_ok=True)

>>> folder.exists()
True

# the folder exists, but we still need to create the empty file
>>> Path('path/not_created_yet/empty.txt').exists()
False

# create it as usual using pathlib touch
>>> Path('path/not_created_yet/empty.txt').touch()

# verify it exists
>>> Path('path/not_created_yet/empty.txt').exists()
True

How to get the filename from path

A Path comes with not only method but also properties. One of them is the Path.name, which as the name implies, returns the filename of the path. This property ignores the parent directories, and return only the file name including the extension.

>>> from pathlib import Path

>>> picture = Path('/home/miguel/Desktop/profile.png')

>>> picture.name
'profile.png'

How to get the filename without the extension

Sometimes, you might need to retrieve the file name without the extension. A natural way of doing this would be splitting the string on the dot. However, pathlib.Path comes with another helper property named Path.stem, which returns the final component of the path, without the extension.

>>> from pathlib import Path

>>> picture = Path('/home/miguel/Desktop/profile.png')

>>> picture.stem
'profile'

How to get the file extension from a filename using pathlib

If the Path.stem property returns the filename excluding the extension, how can we do the opposite? How to retrieve only the extension?

We can do that using the Path.suffix property.

>>> from pathlib import Path

>>> picture = Path('/home/miguel/Desktop/profile.png')

>>> picture.suffix
'.png'

Some files, such as .tar.gz has two parts as extension, and Path.suffix will return only the last part. To get the whole extension, you need the property Path.suffixes.

This property returns a list of all suffixes for that path. We can then use it to join the list into a single string.

>>> backup = Path('/home/miguel/Desktop/photos.tar.gz')

>>> backup.suffix
'.gz'

>>> backup.suffixes
['.tar', '.gz']

>>> ''.join(backup.suffixes)
'.tar.gz'

How to open a file for reading with pathlib

Another great feature from pathlib is the ability to open a file pointed to by the path. The behavior is similar to the built-in open() function. In fact, it accepts pretty much the same parameters.

>>> from pathlib import Path

>>> p = Path('/home/miguel/Desktop/blog/pathlib/recipe.txt')

# open the file
>>> f = p.open()

# read it
>>> lines = f.readlines()

>>> print(lines)
['1. Boil water. \n', '2. Warm up teapot. ...\n', '3. Put tea into teapot and add hot water.\n', '4. Cover teapot and steep tea for 5 minutes.\n', '5. Strain tea solids and pour hot tea into tea cups.\n']

# then make sure to close the file descriptor
>>> f.close()

# or use a context manager, and read the file in one go
>>> with p.open() as f:
             lines = f.readlines()

>>> print(lines)
['1. Boil water. \n', '2. Warm up teapot. ...\n', '3. Put tea into teapot and add hot water.\n', '4. Cover teapot and steep tea for 5 minutes.\n', '5. Strain tea solids and pour hot tea into tea cups.\n']

# you can also read the whole content as string
>>> with p.open() as f:
             content = f.read()


>>> print(content)
1. Boil water.
2. Warm up teapot. ...
3. Put tea into teapot and add hot water.
4. Cover teapot and steep tea for 5 minutes.
5. Strain tea solids and pour hot tea into tea cups.

How to read text files with pathlib

In the previous section, we used the Path.open() method and file.read() function to read the contents of the text file as a string. Even though it works just fine, you still need to close the file or using the with keyword to close it automatically.

pathlib comes with a .read_text() method that does that for you, which is much more convenient.

>>> from pathlib import Path

# just call '.read_text()', no need to close the file
>>> content = p.read_text()

>>> print(content)
1. Boil water.
2. Warm up teapot. ...
3. Put tea into teapot and add hot water.
4. Cover teapot and steep tea for 5 minutes.
5. Strain tea solids and pour hot tea into tea cups.

The file is opened and then closed. The optional parameters have the same meaning as in open(). pathlib docs

How to read JSON files from path with pathlib

A JSON file a nothing more than a text file structured according to the JSON specification. To read a JSON, we can open the path for reading—as we do for text files—and use json.loads() function from the the json module.

>>> import json
>>> from pathlib import Path

>>> response = Path('./jsons/response.json')

>>> with response.open() as f:
        resp = json.load(f)

>>> resp
{'name': 'remi', 'age': 28}

How to read binary files with pathlib

At this point, if you know how to read a text file, then you reading binary files will be easy. We can do this two ways:

  • with the Path.open() method passing the flags rb
  • with the Path.read_bytes() method

Let’s start with the first method.

>>> from pathlib import Path

>>> picture = Path('/home/miguel/Desktop/profile.png')

# open the file
>>> f = picture.open()

# read it
>>> image_bytes = f.read()

>>> print(image_bytes)
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01R\x00\x00\x01p\x08\x02\x00\x00\x00e\xd3d\x85\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00 \x00IDATx\xda\xd4\xbdkw\x1cY\x92\x1ch\xe6~#2\x13\xe0\xa3\xaa\xbbg
...  [OMITTED] ....
0e\xe5\x88\xfc\x7fa\x1a\xc2p\x17\xf0N\xad\x00\x00\x00\x00IEND\xaeB`\x82'

# then make sure to close the file descriptor
>>> f.close()

# or use a context manager, and read the file in one go
>>> with p.open('rb') as f:
            image_bytes = f.read()

>>> print(image_bytes)
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01R\x00\x00\x01p\x08\x02\x00\x00\x00e\xd3d\x85\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00 \x00IDATx\xda\xd4\xbdkw\x1cY\x92\x1ch\xe6~#2\x13\xe0\xa3\xaa\xbbg
...  [OMITTED] ....
0e\xe5\x88\xfc\x7fa\x1a\xc2p\x17\xf0N\xad\x00\x00\x00\x00IEND\xaeB`\x82'

And just like Path.read_text(), pathlib comes with a .read_bytes() method that can open and close the file for you.

>>> from pathlib import Path

# just call '.read_bytes()', no need to close the file
>>> picture = Path('/home/miguel/Desktop/profile.png')

>>> picture.read_bytes()
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01R\x00\x00\x01p\x08\x02\x00\x00\x00e\xd3d\x85\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\x10tEXtSoftware\x00Shutterc\x82\xd0\t\x00\x00 \x00IDATx\xda\xd4\xbdkw\x1cY\x92\x1ch\xe6~#2\x13\xe0\xa3\xaa\xbbg
...  [OMITTED] ....
0e\xe5\x88\xfc\x7fa\x1a\xc2p\x17\xf0N\xad\x00\x00\x00\x00IEND\xaeB`\x82'

How to open all files in a directory in Python

Let’s image you need a Python script to search all files in a directory and open them all. Maybe you want to filter by extension, or you want to do it recursively. If you’ve been following this guide from the beginning, you now know how to use the Path.iterdir() method.

To open all files in a directory, we can combine Path.iterdir() with Path.is_file().

>>> import pathlib
>>> for i in range(2):
        print(i)
# we can use iterdir to traverse all paths in a directory
>>> for path in pathlib.Path("my_images").iterdir():
        # if the path is a file, then we open it
        if path.is_file():
            with path.open(path, "rb") as f:
                image_bytes = f.read()
                load_image_from_bytes(image_bytes)

If you need to do it recursively, we can use Path.rglob() instead of Path.iterdir().

>>> import pathlib
# we can use rglob to walk nested directories
>>> for path in pathlib.Path("my_images").rglob('*'):
        # if the path is a file, then we open it
        if path.is_file():
            with path.open(path, "rb") as f:
                image_bytes = f.read()
                load_image_from_bytes(image_bytes)

How to write a text file with pathlib

In previous sections, we saw how to read text files using Path.read_text().

To write a text file to disk, pathlib comes with a Path.write_text(). The benefits of using this method is that it writes the data and close the file for you, and the optional parameters have the same meaning as in open().

⚠️ WARNING: If you open an existing file, Path.write_text() will overwrite it.

>>> import pathlib

>>> file_path = pathlib.Path('/home/miguel/Desktop/blog/recipe.txt')

>>> recipe_txt = '''
    1. Boil water.
    2. Warm up teapot. ...
    3. Put tea into teapot and add hot water.
    4. Cover teapot and steep tea for 5 minutes.
    5. Strain tea solids and pour hot tea into tea cups.
    '''

>>> file_path.exists()
False

>>> file_path.write_text(recipe_txt)
180

>>> content = file_path.read_text()

>>> print(content)

1. Boil water.
2. Warm up teapot. ...
3. Put tea into teapot and add hot water.
4. Cover teapot and steep tea for 5 minutes.
5. Strain tea solids and pour hot tea into tea cups.

How to write JSON files to path with pathlib

Python represents JSON objects as plain dictionaries, to write them to a file as JSON using pathlib, we need to combine the json.dump function and Path.open(), the same way we did to read a JSON from disk.

>>> import json

>>> import pathlib

>>> resp = {'name': 'remi', 'age': 28}

>>> response = pathlib.Path('./response.json')

>>> response.exists()
False

>>> with response.open('w') as f:
         json.dump(resp, f)


>>> response.read_text()
'{"name": "remi", "age": 28}'

How to write bytes data to a file

To write bytes to a file, we can use either Path.open() method passing the flags wb or Path.write_bytes() method.

>>> from pathlib import Path

>>> image_path_1 = Path('./profile.png')

>>> image_bytes = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00 [OMITTED] \x00I
     END\xaeB`\x82'

>>> with image_path_1.open('wb') as f:
         f.write(image_bytes)


>>> image_path_1.read_bytes()
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00 [OMITTED] \x00IEND\xaeB`\x82'

>>> image_path_2 = Path('./profile_2.png')

>>> image_path_2.exists()
False

>>> image_path_2.write_bytes(image_bytes)
37

>>> image_path_2.read_bytes()
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00 [OMITTED] \x00IEND\xaeB`\x82'

How to copy files with pathlib

pathlib cannot copy files. However, if we have a file represented by a path that doesn’t mean we can’t copy it. There are two different ways of doing that:

  • using the shutil module
  • using the Path.read_bytes() and Path.write_bytes() methods

For the first alternative, we use the shutil.copyfile(src, dst) function and pass the source and destination path.

>>> import pathlib, shutil

>>> src = Path('/home/miguel/Desktop/blog/pathlib/sandbox/article.txt')

>>> src.exists()
True

>>> dst = Path('/home/miguel/Desktop/blog/pathlib/sandbox/reports/article.txt')

>>> dst.exists()
>>> False

>>> shutil.copyfile(src, dst)
PosixPath('/home/miguel/Desktop/blog/pathlib/sandbox/reports/article.txt')

>>> dst.exists()
True

>>> dst.read_text()
'This is \n\nan \n\ninteresting article.\n'

>>> dst.read_text() == src.read_text()
True

⚠️ WARNING: shutil prior to Python 3.6 cannot handle Path instances. You need to convert the path to string first.

The second method involves copying the whole file, then writing it to another destination.

>>> import pathlib, shutil

>>> src = Path('/home/miguel/Desktop/blog/pathlib/sandbox/article.txt')

>>> src.exists()
True

>>> dst = Path('/home/miguel/Desktop/blog/pathlib/sandbox/reports/article.txt')

>>> dst.exists()
False

>>> dst.write_bytes(src.read_bytes())
36

>>> dst.exists()
True

>>> dst.read_text()
'This is \n\nan \n\ninteresting article.\n'

>>> dst.read_text() == src.read_text()
True

⚠️ WARNING: This method will overwrite the destination path. If that’s a concern, it’s advisable either to check if the file exists first, or to open the file in writing mode using the x flag. This flag will open the file exclusive creation, thus failing with FileExistsError if the file already exists.

Another downside of this approach is that it loads the file to memory. If the file is big, prefer shutil.copyfileobj. It supports buffering and can read the file in chunks, thus avoiding uncontrolled memory consumption.

>>> import pathlib, shutil

>>> src = Path('/home/miguel/Desktop/blog/pathlib/sandbox/article.txt')
>>> dst = Path('/home/miguel/Desktop/blog/pathlib/sandbox/reports/article.txt')

>>> if not dst.exists():
         dst.write_bytes(src.read_bytes())
     else:
         print('File already exists, aborting...')

File already exists, aborting...

>>> with dst.open('xb') as f:
         f.write(src.read_bytes())

---------------------------------------------------------------------------
FileExistsError                           Traceback (most recent call last)
<ipython-input-25-1974c5808b1a> in <module>
----> 1 with dst.open('xb') as f:
      2     f.write(src.read_bytes())
      3

How to delete a file with pathlib

You can remove a file or symbolic link with the Path.unlink() method.

>>> from pathlib import Path

>>> Path('path/reports/report.csv').touch()

>>> path = Path('path/reports/report.csv')

>>> path.exists()
True

>>> path.unlink()

>>> path.exists()
False

As of Python 3.8, this method takes one argument named missing_ok. By default, missing_ok is set to False, which means it will raise an FileNotFoundError error if the file doesn’t exist.

>>> path = Path('path/reports/report.csv')

>>> path.exists()
False

>>> path.unlink()
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-6-8eea53121d7f> in <module>
----> 1 path.unlink()

~/.pyenv/versions/3.9.4/lib/python3.9/pathlib.py in unlink(self, missing_ok)
   1342         try:
-> 1343             self._accessor.unlink(self)
   1344         except FileNotFoundError:
   1345             if not missing_ok:

FileNotFoundError: [Errno 2] No such file or directory: 'path/reports/report.csv'

# when missing_ok is True, no error is raised
>>> path.unlink(missing_ok=True)

How to delete all files in a directory with pathlib

To remove all files in a folder, we need to traverse it and check if the path is a file, and if so, call Path.unlink() on it as we saw in the previous section.

To walk over the contents of a directory, we can use Path.iterdir(). Let’s consider the following directory.

$ tree /home/miguel/path/
/home/miguel/path/
├── jsons
│   └── response.json
├── new_parent_dir
│   └── sub_dir
├── non_empty_dir
│   └── file.txt
├── not_created_yet
│   └── empty.txt
├── number.csv
├── photo_1.png
├── report.md
└── reports

This method only deletes the immediate files under the current directory, so it is not recursive.

>>> import pathlib

>>> path = pathlib.Path('/home/miguel/path')

>>> list(path.iterdir())
Out[5]:
[PosixPath('/home/miguel/path/jsons'),
 PosixPath('/home/miguel/path/non_empty_dir'),
 PosixPath('/home/miguel/path/not_created_yet'),
 PosixPath('/home/miguel/path/reports'),
 PosixPath('/home/miguel/path/photo_1.png'),
 PosixPath('/home/miguel/path/number.csv'),
 PosixPath('/home/miguel/path/new_parent_dir'),
 PosixPath('/home/miguel/path/report.md')]

>>> for p in path.iterdir():
        if p.is_file():
            p.unlink()


>>> list(path.iterdir())
[PosixPath('/home/miguel/path/jsons'),
 PosixPath('/home/miguel/path/non_empty_dir'),
 PosixPath('/home/miguel/path/not_created_yet'),
 PosixPath('/home/miguel/path/reports'),
 PosixPath('/home/miguel/path/new_parent_dir')]

How to rename a file using pathlib

pathlib also comes with a method to rename files called Path.rename(target). It takes a target file path and renames the source to the target. As of Python 3.8, Path.rename() returns the new Path instance.

>>> from pathlib import Path

>>> src_file = Path('recipe.txt')

>>> src_file.open('w').write('An delicious recipe')
19
>>> src_file.read_text()
'An delicious recipe'

>>> target = Path('new_recipe.txt')

>>> src_file.rename(target)
PosixPath('new_recipe.txt')

>>> src_file
PosixPath('recipe.txt')

>>> src_file.exists()
False

>>> target.read_text()
'An delicious recipe'

Renaming only file extension

If all you want is to change the file extension to something else, for example, change from .txt to .md, you can use Path.rename(target) in conjunction with Path.with_suffix(suffix) method, which does the following:

  • appends a new suffix, if the original path doesn’t have one
  • removes the suffix, if the supplied suffix is an empty string

Let’s see an example where we change our recipe file from plain text .txt to markdown .md.

>>> from pathlib import Path

>>> src_file = Path('recipe.txt')

>>> src_file.open('w').write('An delicious recipe')
19

>>> new_src_file = src_file.rename(src_file.with_suffix('.md'))

>>> new_src_file
PosixPath('recipe.md')

>>> src_file.exists()
False

>>> new_src_file.exists()
True

>>> new_src_file.read_text()
'An delicious recipe'

>>> removed_extension_file = new_src_file.rename(src_file.with_suffix(''))

>>> removed_extension_file
PosixPath('recipe')

>>> removed_extension_file.read_text()
'An delicious recipe'

How to get the parent directory of a file with pathlib

Sometimes we want to get the name of the directory a file belongs to. You can get that through a Path property named parent. This property represents the logical parent of the path, which means it returns the parent of a file or directory.

>>> from pathlib import Path

>>> path = Path('path/reports/report.csv')

>>> path.exists()
False

>>> parent_dir = path.parent

>>> parent_dir
PosixPath('path/reports')

>>> parent_dir.parent
PosixPath('path')

Conclusion

That was a lot to learn, and I hope you enjoyed it just as I enjoyed writing it.

pathlib has been part of the standard library since Python 3.4 and it’s a great solution when it comes to handling paths.

In this guide, we covered the most important use cases in which pathlib shines through tons of examples.

I hope this cookbook is useful to you, and see you next time.

Other posts you may like:

  • Find the Current Working Directory in Python

  • The Best Ways to Compare Two Lists in Python

  • Python F-String: 73 Examples to Help You Master It

See you next time!

This article was originally published at https://miguendes.me

Table of Contents

  • 1 How do I convert a Windows path to a string in Python?
  • 2 How do you create an absolute path in Python?
  • 3 What does path () do in Python?
  • 4 What does path do Python?
  • 5 What does OS path Dirname do?
  • 6 How does the OS path module in Python work?
  • 7 How to import an OS module in Python?
  • 8 How does the OS module work in Python?
  • 9 What’s the difference between OS and OS in Python?

“python convert WindowsPath to string” Code Answer

  1. >>> import pathlib.
  2. >>> p = pathlib. PureWindowsPath(r’\dir\anotherdir\foodir\more’)
  3. >>> print(p)
  4. \dir\anotherdir\foodir\more.
  5. >>> print(p. as_posix())
  6. /dir/anotherdir/foodir/more.
  7. >>> str(p)
  8. ‘\\dir\\anotherdir\\foodir\\more’

How do you create an absolute path in Python?

Use abspath() to Get the Absolute Path in Python To get the absolute path using this module, call path. abspath() with the given path to get the absolute path. The output of the abspath() function will return a string value of the absolute path relative to the current working directory.

How do you create a relative path in Python?

path. relpath() method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory. Note: This method only computes the relative path.

How do you change OS path in Python?

How to change current working directory in python?

  1. import os. import os. import os.
  2. os. chdir(path) os.chdir(path)
  3. print(“Current Working Directory ” , os. getcwd()) print(“Current Working Directory ” , os.getcwd())
  4. os. chdir(“/home/varun/temp”) os.chdir(“/home/varun/temp”)

What does path () do in Python?

To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest: Notice two things here: You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.

What does path do Python?

The Path variable lists the directories that will be searched for executables when you type a command in the command prompt. By adding the path to the Python executable, you will be able to access python.exe by typing the python keyword (you won’t need to specify the full path to the program).

How do I open a relative path in Python?

Open file in a relative location in Python

  1. You’re using unescaped backslashes. That’s one disadvantage. –
  2. Several disadvantages.
  3. And for good measure…
  4. beside the necessary care on slashes, as just indicated, there is the function os.path.abspath to get easly the full path of the relative path to open.

What is a relative path Python?

The relative path is the path to some file with respect to your current working directory (PWD). For example: Absolute path: C:/users/admin/docs/stuff.txt. If my PWD is C:/users/admin/ , then the relative path to stuff.txt would be: docs/stuff.txt. Note, PWD + relative path = absolute path.

What does OS path Dirname do?

path. dirname() method in Python is used to get the directory name from the specified path.

How does the OS path module in Python work?

All of these functions accept either only bytes or only string objects as their parameters. The result is an object of the same type, if a path or file name is returned. As there are different versions of operating system so there are several versions of this module in the standard library.

What is an example of os.path.join in Python?

Let’s take the following path as an example: This path takes us to a folder called “tutorials”. If we wanted to access a particular file or directory in this folder, we could point to it using its file name: You can write these file paths manually in Python. Doing so can be impractical. That’s where os.path.join comes in.

How is the OS method used in Python?

os.makedirs () method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs () method will create them all. os.listdir () method in Python is used to get the list of all files and directories in the specified directory.

How to import an OS module in Python?

Write the following code to import the OS module. If you do not know what is a module in Python, then you can check out this Python Module article. Now, let’s see some of the essential functions of os in detail. The os.name function gives the name of the OS module it imports. This differs based on the underlying Operating System. See the output.

How does the OS module work in Python?

This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.dirname () method in Python is used to get the directory name from the specified path. path: A path-like object representing a file system path.

Where does import os.path go in Python?

sys.modules is a dict in which modules are cached. When you import a module, if it already has been imported somewhere, it gets the instance stored in sys.modules. os is among the modules that are loaded when Python starts up. It assigns its path attribute to an os-specific path module.

Which is the correct way to use OS path join in Python?

I came across this python function os.path.join (). I wanted to know which was a preferred method of using it. Thanks for contributing an answer to Stack Overflow!

What’s the difference between OS and OS in Python?

It looks like os should be a package with a submodule path, but in reality os is a normal module that does magic with sys.modules to inject os.path. Here’s what happens: When Python starts up, it loads a bunch of modules into sys.modules.

You can use the str() function to convert a WindowsPath object to a string in Python. Here's an example:

python
from pathlib import Path

path = Path("C:/Users/username/Documents/example.txt")
string_path = str(path)

print(string_path)


Output:


C:\Users\username\Documents\example.txt

Converting WindowsPath to a String in Python

To convert a WindowsPath object to a string in Python, you can use the str() function. Here's an example:

python
from pathlib import Path

# create a WindowsPath object
path = Path("C:/Users/username/Desktop/file.txt")

# convert it to a string
path_str = str(path)

# print the result
print(path_str)


This will output:


C:\Users\username\Desktop\file.txt

Benefits of Converting WindowsPath to a String

There are a few potential benefits to converting a WindowsPath object to a string in Python:

1. Compatibility: Some Python libraries or functions may not accept a WindowsPath object as input, but may require a string. In these cases, converting the WindowsPath object to a string can make it possible to use those functions or libraries.

2. Ease of use: If a WindowsPath object is being used in many different places throughout a script or program, converting it to a string can make it easier to work with, since strings are a more commonly used data type.

3. Flexibility: Converting a WindowsPath object to a string allows more flexibility in how the data can be manipulated. For example, it can be easily split, concatenated with other strings, or otherwise modified to fit different use cases.

4. Readability: Depending on the formatting of the WindowsPath object, converting it to a string can make the path more readable and easier to understand.

Examples of Converting WindowsPath to a String

Here are a few examples of converting WindowsPath to a string in Python:

1. Using the str() function - Simply pass the WindowsPath object to the str() function to convert it to a string.


from pathlib import Path

path = Path(r'C:\Users\mypc\Documents\example.txt')
string_path = str(path)

print(string_path)


Output:

C:\Users\mypc\Documents\example.txt


2. Using the as_posix() method - This method converts the WindowsPath to a POSIX-style path and returns it as a string.


from pathlib import Path

path = Path(r'C:\Users\mypc\Documents\example.txt')
string_path = path.as_posix()

print(string_path)


Output:

C:/Users/mypc/Documents/example.txt


3. Using the join() method - If you have multiple components of the path and want to join them into a single string, you can use the join() method.


from pathlib import Path

path = Path(r'C:\Users\mypc\Documents')
file_name = 'example.txt'
string_path = str(path.joinpath(file_name))

print(string_path)


Output:

C:\Users\mypc\Documents\example.txt

Conclusion:

In conclusion, converting WindowsPath to a string in Python is a quick and easy way to simplify file input/output operations. Benefits include increased readability and compatibility across different platforms. While the process may seem daunting for newcomers, it can quickly become second nature with just a bit of practice. By implementing the tips and examples discussed in this article, you’ll be on your way to successfully integrating this functionality into your own projects in no time.



  • Python path переменная среда windows 10
  • Python skachat windows 10 pro
  • Python path to file windows
  • Python path not found windows
  • Python org downloads windows 10