python开发_os.path

时间:2024-09-03 17:35:44

python中,os.path模块在处理路径的时候非常有用

下面是我做的demo

运行效果:

python开发_os.path

=========================================

代码部分:

=========================================

 #python os

 import os

 def abspath(path):
'''Return a normalized absolutized version of the pathname path'''
return os.path.abspath(path) def dirname(path):
'''Return the directory name of pathname path'''
return os.path.dirname(path) def getatime(path):
'''Return the time of last access of path'''
return os.path.getatime(path) def gettime(path):
'''Return the time of last modification of path'''
return os.path.gettime(path) def getsize(path):
'''Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.'''
return os.path.getsize(path) def is_file(path):
'''Return True if path is an existing regular file.
This follows symbolic links, so both islink() and isfile()
can be true for the same path.'''
return os.path.isfile(path) def is_dir(path):
'''Return True if path is an existing directory. This follows symbolic links,
so both islink() and isdir() can be true for the same path.'''
return os.path.isdir(path) def is_link(path):
'''Return True if path refers to a directory entry that
is a symbolic link. Always False if symbolic links are not supported.'''
return os.path.islink(path) def splitext(path):
'''
Split the pathname path into a pair (root, ext) such that
root + ext == path, and ext is empty or begins with a period
and contains at most one period. Leading periods on the basename
are ignored; splitext('.cshrc') returns ('.cshrc', '').
'''
return os.path.splitext(path) def splitunc(path):
'''
Split the pathname path into a pair (unc, rest) so that unc is
the UNC mount point (such as r'\\host\mount'), if present,
and rest the rest of the path (such as r'\path\file.ext').
For paths containing drive letters, unc will always be the
empty string.
'''
return os.path.splitunc(path) def split(path):
'''Split the pathname path into a pair, (head, tail) where tail is the last
pathname component and head is everything leading up to that'''
return os.path.split(path) def main():
path_file = 'C:\\test.html'
path_dir = 'C:\\Windows\\Branding'
print(abspath(path_file))
print(dirname(path_dir))
print(getatime(path_file))
print(getsize(path_file))
print(splitext(path_file))
print(splitunc(path_file))
print(split(path_file)) if __name__ == '__main__':
main()