Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2023-03-01 14:38:24
Size: 1604
Comment:
Revision 5 as of 2023-03-02 16:03:36
Size: 2964
Comment:
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
== Path == == Usage ==



=
== Path ===
Line 18: Line 22:

=== Absolute ===

=== Anchor ===

=== As_Posix ===
----



=
=== Absolute ====

----



=
=== Anchor ====

----



=
=== As_Posix ====

----

Line 27: Line 46:
----


Line 29: Line 52:
----


Line 31: Line 58:
----


Line 33: Line 64:
----


Line 35: Line 70:
----


Line 37: Line 76:
----


Line 39: Line 82:
----


Line 41: Line 88:
----


Line 43: Line 94:
----


Line 45: Line 100:
----


Line 47: Line 106:
----


Line 49: Line 112:
----


Line 51: Line 118:
----


Line 53: Line 124:
----


Line 55: Line 130:
----


Line 57: Line 136:
----


Line 59: Line 142:
----


Line 61: Line 148:
----


Line 63: Line 154:
----


Line 65: Line 160:
----


Line 67: Line 166:
Takes one or more strings or `Path` objects.

{{{
from pathlib import Path
p = Path('.')

t = p.joinpath('temp')
}}}

----


Line 69: Line 181:
----


Line 71: Line 187:
{{{
from pathlib import Path
p = Path('.')

t = p.joinpath('temp')
t.mkdir(parents=False, exist_ok=True)
}}}

If a parent directory does not exist, `FileNotFoundError` is raised. If the `parents` option is set to `True` though, the missing parents are recursively `mkdir`ed instead.

If the directory already exists, `FileExistsError` is raised. If the `exist_ok` option is set to `True` though, this error is suppressed. If a file already exists at the named location, `FileExistsError` is raised regardless of the `exist_ok` option.

----


Line 73: Line 205:
----


Line 75: Line 211:
----


Line 77: Line 217:
----


Line 79: Line 223:
----


Line 81: Line 229:
----


Line 83: Line 235:
----


Line 85: Line 241:
----


Line 87: Line 247:
----


Line 89: Line 253:
----


Line 91: Line 259:
----


Line 93: Line 265:
----


Line 95: Line 271:
----


Line 97: Line 277:
----


Line 99: Line 283:
----


Line 101: Line 289:
----


Line 103: Line 295:
----


Line 105: Line 301:
----


Line 107: Line 307:
----


Line 109: Line 313:
----


Line 111: Line 319:
----


Line 113: Line 325:
----


Line 115: Line 331:
----


Line 117: Line 337:
----


Line 119: Line 343:
----


Line 121: Line 349:
----


Line 123: Line 355:
----


Line 125: Line 361:
----


Line 126: Line 366:

----

Python Pathlib


Usage

Path

The Path class is instantiated like:

from pathlib import Path
p = Path('.')


Absolute


Anchor


As_Posix


As_Uri


ChMod


Drive


Exists


ExpandUser


Glob


Group



Is_Absolute


Is_Block_Device


Is_Char_Device


Is_Dir


Is_Fifo


Is_File


Is_Mount


Is_Relative_To


Is_Reserved


Is_Socket



IterDir


JoinPath

Takes one or more strings or Path objects.

from pathlib import Path
p = Path('.')

t = p.joinpath('temp')


Match


MkDir

from pathlib import Path
p = Path('.')

t = p.joinpath('temp')
t.mkdir(parents=False, exist_ok=True)

If a parent directory does not exist, FileNotFoundError is raised. If the parents option is set to True though, the missing parents are recursively mkdired instead.

If the directory already exists, FileExistsError is raised. If the exist_ok option is set to True though, this error is suppressed. If a file already exists at the named location, FileExistsError is raised regardless of the exist_ok option.


Name


Open


Owner


Parent


Parents


Parts


Read_Bytes


Read_Text



Relative_To


Rename


Replace


Resolve


RGlob


RmDir


Root


SameFile


Stat


Stem


Suffix


Suffixes



Touch



With_Name


With_Stem


With_Suffix


Write_Bytes


Write_Text


Cwd

A Path class method.

from pathlib import Path
c = Path.cwd()


Home

A Path class method.

from pathlib import Path
h = Path.home()


See also

Python pathlib module documentation


CategoryRicottone

Python/PathLib (last edited 2023-10-11 19:40:58 by DominicRicottone)