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

Python os.path Module

Python os.path Module

Python File methods Python OS file/directory methods


The os.path module is mainly used to get the properties of files.

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

Method Description
os.path.abspath(path) Return the absolute path
os.path.basename(path) Return the file name
os.path.commonprefix(list) Return the longest path common to all the paths in a list (multiple paths)
os.path.dirname(path) Return the file path
os.path.exists(path) Return True if the path exists; return False if the path does not exist or is broken.
os.path.lexists(path) Return True if the path exists, and also return True if the path is broken
os.path.expanduser(path) Convert the ~ and ~user in the path to the user's home directory
os.path.expandvars(path) Replace according to the value of environment variables path the $name and ${name} contained in it
os.path.getatime(path) Return the most recent access time (floating-point seconds)
os.path.getmtime(path) Return the most recent file modification time
os.path.getctime(path) Return the creation time of the file path
os.path.getsize(path) Return the file size; if the file does not exist, return an error
os.path.isabs(path) Determine whether it is an absolute path
os.path.isfile(path) Determine whether the path is a file
os.path.isdir(path) Determine whether the path is a directory
os.path.islink(path) Determine whether the path is a link
os.path.ismount(path) Determine whether the path is a mount point
os.path.join(path1[, path2[, ...]]) Combine a directory and a filename into one path
os.path.normcase(path) Convert the case and slashes of the path
os.path.normpath(path) Normalize the string form of path
os.path.realpath(path) Return the real path of the path
os.path.relpath(path[, start]) Compute a relative path starting from start
os.path.samefile(path1, path2) Determine whether two directories or files are the same
os.path.sameopenfile(fp1, fp2) Determine whether fp1 and fp2 point to the same file
os.path.samestat(stat1, stat2) Determine whether the stat tuples stat1 and stat2 point to the same file
os.path.split(path) Split the path into dirname and basename, returning a tuple
os.path.splitdrive(path) Generally used on Windows, return a tuple consisting of the drive name and the path
os.path.splitext(path) Split the path, returning a tuple of the path name and the file extension
os.path.splitunc(path) Split the path into a mount point and a file
os.path.walk(path, visit, arg) Traverse the path, calling the visit function upon entering each directory. The visit function must have three parameters (arg, dirname, names), where dirname is the directory name of the current directory, names represents all the filenames in the current directory, and args is the third parameter of walk.
os.path.supports_unicode_filenames Set whether unicode path names are supported

Example

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

Example

#!/usr/bin/python

-- coding: UTF-8 --

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

#!/usr/bin/python

-- coding: UTF-8 --

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

Python File methods Python OS file/directory methods