Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2023-03-01 14:38:24
Size: 1604
Comment:
Revision 4 as of 2023-03-01 14:50:50
Size: 2937
Comment:
Deletions are marked like this. Additions are marked like this.
Line 18: Line 18:
----

Line 21: Line 24:
----


Line 23: Line 30:
----


Line 25: Line 36:
----


Line 27: Line 42:
----


Line 29: Line 48:
----


Line 31: Line 54:
----


Line 33: Line 60:
----


Line 35: Line 66:
----


Line 37: Line 72:
----


Line 39: Line 78:
----


Line 41: Line 84:
----


Line 43: Line 90:
----


Line 45: Line 96:
----


Line 47: Line 102:
----


Line 49: Line 108:
----


Line 51: Line 114:
----


Line 53: Line 120:
----


Line 55: Line 126:
----


Line 57: Line 132:
----


Line 59: Line 138:
----


Line 61: Line 144:
----


Line 63: Line 150:
----


Line 65: Line 156:
----


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

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

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

----


Line 69: Line 177:
----


Line 71: Line 183:
{{{
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 201:
----


Line 75: Line 207:
----


Line 77: Line 213:
----


Line 79: Line 219:
----


Line 81: Line 225:
----


Line 83: Line 231:
----


Line 85: Line 237:
----


Line 87: Line 243:
----


Line 89: Line 249:
----


Line 91: Line 255:
----


Line 93: Line 261:
----


Line 95: Line 267:
----


Line 97: Line 273:
----


Line 99: Line 279:
----


Line 101: Line 285:
----


Line 103: Line 291:
----


Line 105: Line 297:
----


Line 107: Line 303:
----


Line 109: Line 309:
----


Line 111: Line 315:
----


Line 113: Line 321:
----


Line 115: Line 327:
----


Line 117: Line 333:
----


Line 119: Line 339:
----


Line 121: Line 345:
----


Line 123: Line 351:
----


Line 125: Line 357:
----


Line 126: Line 362:

----

Python Pathlib


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)