Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.path() Module

Python3 os.path() Module

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


os.path module is part of the os module in the Python standard library, dedicated to manipulating and handling file paths.

os.path It provides a powerful set of tools for various operations on file and directory paths, such as getting a file name, checking whether a path exists, joining paths and normalizing paths.

os.path The module behaves well across operating systems, allowing the same code to handle path-related issues when running on different operating systems (such as Windows, Linux and macOS).

The following are several commonly used methods of the os.path module:

Path operations

Method Description
os.path.abspath(path) Convert a relative path into an absolute path.
os.path.basename(path) Get the last part of a path, that is, the file name.
os.path.dirname(path) Get the directory part of a path.
os.path.join(*paths) Join several paths, handling path separators automatically.
os.path.split(path) Split a path into a tuple of directory and file name.
os.path.splitext(path) Split a path into a tuple of file name and extension.

Getting path information

Method Description
os.path.exists(path) Check whether a path exists.
os.path.isfile(path) Check whether a path is a file.
os.path.isdir(path) Check whether a path is a directory.
os.path.getsize(path) Get the size of a file in bytes.
os.path.getatime(path) Get the last access time of a file.
os.path.getmtime(path) Get the last modification time of a file.
os.path.getctime(path) Get the creation time of a file (on some operating systems it means the time of the last status change).

Path normalization

Method Description
os.path.normpath(path) Normalize a path, removing redundant separators and relative path markers.
os.path.realpath(path) Get the real path of a file, resolving symbolic links.
os.path.relpath(path, start=os.curdir) Compute a relative path from start to path。

Path comparison

Method Description
os.path.commonpath(paths) Return the common path in a sequence of paths.
os.path.commonprefix(list) Return the longest common prefix in a sequence of paths.
os.path.samefile(path1, path2) Check whether two paths point to the same file.
os.path.sameopenfile(fp1, fp2) Check whether two open file objects point to the same file.
os.path.samestat(stat1, stat2) Check whether two files have the same stat status.

Platform-dependent functions

Method Description
os.path.splitdrive(path) On Windows, return a tuple of the drive/device part and the path part of a path.
os.path.splitunc(path) Split the shared device and path parts of a path (Windows only).

Example

The following example demonstrates the use of os.path related methods:

Example

#!/usr/bin/env python

import

os

.

path

Current file name

print

(

file

)

Absolute path of the current file name

print

(

os

.

path

.

abspath

(

file

)

)

Return the path of the current file

print

(

os

.

path

.

dirname

(

os

.

path

.

abspath

(

file

)

)

)

The following example outputs information about a file.

test.py
/runoob/runoob-test-py/test.py
/runoob/runoob-test-py

Example

#!/usr/bin/python3

import

os

print

(

os

.

path

.

basename

(

'

/root/runoob.txt

'

)

)

Return the file name

print

(

os

.

path

.

dirname

(

'

/root/runoob.txt

'

)

)

Return the directory path

print

(

os

.

path

.

split

(

'

/root/runoob.txt

'

)

)

Split the file name from the path

print

(

os

.

path

.

join

(

'

root

'

,

'

test

'

,

'

runoob.txt

'

)

)

Combine a directory and a file name into one path

The output of the above program is:


runoob.txt
/root
('/root', 'runoob.txt')
root/test/runoob.txt

The following example outputs information about a file.

Example

import

os

import

time

file

=

'

/root/runoob.txt

'

File path

print

(

os

.

path

.

getatime

(

file

)

)

Output the most recent access time

print

(

os

.

path

.

getctime

(

file

)

)

Output the file creation time

print

(

os

.

path

.

getmtime

(

file

)

)

Output the most recent modification time

print

(

time

.

gmtime

(

os

.

path

.

getmtime

(

file

)

)

)

Output the most recent modification time as struct_time

print

(

os

.

path

.

getsize

(

file

)

)

Output the file size (in bytes)

print

(

os

.

path

.

abspath

(

file

)

)

Output absolute path

print

(

os

.

path

.

normpath

(

file

)

)

Normalize the string form of path

The output of the above program is:

1539052805.5735736
1539052805.5775735
1539052805.5735736
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=9, tm_hour=2, tm_min=40, tm_sec=5, tm_wday=1, tm_yday=282, tm_isdst=0)
7
/root/runoob.txt
/root/runoob.txt

Python3 OS File/Directory Methods Python3 OS File/Directory Methods