Librería os.path (manipulaciones comunes del pathname)#
Última modificación: Mayo 13, 2022
[1]:
import os.path
[2]:
# -----------------------------------------------------------------------------
# Return the base name of pathname path.
#
os.path.basename('/tmp/user/text.txt')
[2]:
'text.txt'
[3]:
# -----------------------------------------------------------------------------
# Return the longest common sub-path of each pathname in the sequence paths.
#
os.path.commonpath(
[
'/tmp/user/text0.txt',
'/tmp/user/text1.txt',
]
)
[3]:
'/tmp/user'
[4]:
# -----------------------------------------------------------------------------
# Return the longest path prefix (taken character-by-character) that is a
# prefix of all paths in list.
os.path.commonprefix(
[
'/tmp/user/text0.txt',
'/tmp/user/text1.txt',
]
)
[4]:
'/tmp/user/text'
[5]:
# -----------------------------------------------------------------------------
# Return the directory name of pathname path.
#
os.path.dirname('/tmp/user/text.txt')
[5]:
'/tmp/user'
[6]:
# -----------------------------------------------------------------------------
# Return True if path refers to an existing path or an open file descriptor.
#
os.path.exists('/tmp/user/text.txt')
[6]:
False
[7]:
# -----------------------------------------------------------------------------
# On Unix and Windows, return the argument with an initial component of ~ or
# ~user replaced by that user’s home directory.
#
os.path.expanduser('~')
[7]:
'/root'
[8]:
# -----------------------------------------------------------------------------
# Return the argument with environment variables expanded.
#
os.path.expandvars('$PATH')
[8]:
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
[9]:
# -----------------------------------------------------------------------------
# Return the time of last access of path.
#
!touch '/tmp/demo.txt'
os.path.getatime('/tmp/demo.txt')
[9]:
1650556566.2562885
[10]:
# -----------------------------------------------------------------------------
# Return True if path is an absolute pathname.
#
display(
os.path.isabs('./demo.txt'),
os.path.isabs('/tmp/demo.txt'),
)
False
True
[11]:
# -----------------------------------------------------------------------------
# Return True if path is an existing regular file.
#
os.path.isfile('/tmp/demo.txt')
[11]:
True
[12]:
# -----------------------------------------------------------------------------
# Return True if path is an existing directory.
#
os.path.isdir('/tmp/demo.txt')
[12]:
False
[13]:
# -----------------------------------------------------------------------------
# Join one or more path components intelligently.
#
os.path.join('/tmp', 'demo', 'a.txt')
[13]:
'/tmp/demo/a.txt'
[14]:
os.path.join('/tmp', 'demo/', 'a.txt')
[14]:
'/tmp/demo/a.txt'
[15]:
os.path.join('/tmp', '/demo/', 'a.txt')
[15]:
'/demo/a.txt'
[16]:
# -----------------------------------------------------------------------------
# solo cambia los caracteres en windows
#
os.path.normcase('/tmp/FILE.txt')
[16]:
'/tmp/FILE.txt'
[17]:
# -----------------------------------------------------------------------------
# Normalize a pathname by collapsing redundant separators and up-level
# references so that A//B, A/B/, A/./B and A/foo/../B all become A/B.
#
os.path.normpath('/tmp//./FILE.txt')
[17]:
'/tmp/FILE.txt'
[18]:
# -----------------------------------------------------------------------------
# Return the canonical path of the specified filename, eliminating any symbolic
# links encountered in the path
#
os.path.realpath('FILE.txt')
[18]:
'/workspace/the_python_tutorial_10_brief_tour_of_the_standard_library/FILE.txt'
[19]:
os.path.realpath('./FILE.txt')
[19]:
'/workspace/the_python_tutorial_10_brief_tour_of_the_standard_library/FILE.txt'
[20]:
# -----------------------------------------------------------------------------
# Split the pathname path into a pair, (head, tail) where tail is the last
# pathname component and head is everything leading up to that.
#
os.path.split('/tmp/system/demo.txt')
[20]:
('/tmp/system', 'demo.txt')