"pathlib.Path" - Read and Write Operations

This section describes methods from the 'pathlib.Path' class for file read and write operations.

"pathlib.Path" class also offers a number of methods to perform read and write operations on files:

Here are examples on how to use above pathlib.Path methods.

>>> from pathlib import Path

# Loop through child items of a directory
>>> p = Path('docs')
>>> for child in p.iterdir(): child
...
PosixPath('docs/conf.py')
PosixPath('docs/_templates')
PosixPath('docs/make.bat')
PosixPath('docs/index.rst')
PosixPath('docs/_build')
PosixPath('docs/_static')
PosixPath('docs/Makefile')

# write text, rename and read text on a file.
>>> p = Path('foo')
>>> p.write_text('some text')
9
>>> target = Path('bar')
>>> p.rename(target)
PosixPath('bar')
>>> target.read_text()
'some text'

>>> target.unlink()

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators and Generators

 List, Set and Dictionary Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

"pathlib" - Object-Oriented Filesystem Paths

 What Is "pathlib" Module

 "pathlib.Path" - Path for Both Linux and Windows

"pathlib.Path" - Read and Write Operations

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB