python os.path.join(), os.path.basename(), os.path.dirname(), os.path.split() usage

 

# -*- coding:utf-8 -*
import os

Path1 = 'Python'
Path2 = 'Java'
Path3 = 'C++'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1, Path2, Path3)
print('Path1 = ', Path10)
print('Path1 = ', Path20)

Path1 = '/Python'
Path2 = 'Java'
Path3 = 'C++'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1, Path2, Path3)
print('Path2 = ', Path10)
print('Path2 = ', Path20)

Path1 = 'Python'
Path2 = '/Java'
Path3 = 'C++'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1, Path2, Path3)
print('Path3 = ', Path10)
print('Path3 = ', Path20)

Path1 = 'Python'
Path2 = 'Java'
Path3 = './C++'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1, Path2, Path3)
print('Path4 = ', Path10)
print('Path4 = ', Path20)

Path1 = 'Python'
Path2 = ' /Java'
Path3 = 'C++'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1, Path2, Path3)
print('Path5 = ', Path10)
print('Path5 = ', Path20)

Path1 = 'Python'
Path2 = 'Java'
Path3 = 'C++'
Path4 = '/runoob.txt'

Path10 = os.path.basename(Path1 + Path2 + Path3 + Path4)  # 返回文件名
Path20 = os.path.dirname(Path1 + Path2 + Path3 + Path4)  # 返回目录路径
Path30 = os.path.split(Path1 + Path2 + Path3 + Path4)  # 分割文件名与路径
print('Path6 = ', Path10)
print('Path6 = ', Path20)
print('Path6 = ', Path30)

 Run results, windows and linux results will be a bit different

method Description
os.path.abspath(path) Return absolute path
os.path.basename(path) Return file name
os.path.commonprefix(list) Returns the longest path shared by all paths in the list (multiple paths)
os.path.dirname(path) Return file path
os.path.exists(path) If path exists, return True; if path does not exist, return False.
os.path.lexists Return True if the path exists, and True if the path is damaged
os.path.expanduser(path) Convert the "~" and "~user" contained in the path into user directories
os.path.expandvars(path) Replace "$name" and "${name}" contained in path according to the value of environment variable
os.path.getatime (path) Returns the most recent access time (floating point number of seconds)
os.path.getmtime(path) Return the last file modification time
os.path.getctime(path) Return file path creation time
os.path.getsize(path) Returns the file size, if the file does not exist, returns 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 directory and file name into a path
os.path.normcase(path) Convert case and slash of path
os.path.normpath(path) Canonical path string form
os.path.realpath(path) Return the real path of path
os.path.relpath(path[, start]) Calculate the relative path from start
os.path.samefile(path1, path2) Determine whether the 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 stat tuple stat1 and stat2 point to the same file
os.path.split(path) Split the path into dirname and basename and return a tuple
os.path.splitdrive(path) Generally used under windows, it returns a tuple of drive name and path
os.path.splitext(path) Split the path and return a tuple of pathname and file extension
os.path.splitunc(path) Split the path into load points and files
os.path.walk(path, visit, arg) Traverse the path and call the visit function when entering each directory. The visit function must have 3 parameters (arg, dirname, names). dirname represents the directory name of the current directory, names represents all the file names in the current directory, and args is walk The third parameter
os.path.supports_unicode_filenames Set whether to support unicode path names

Guess you like

Origin blog.csdn.net/weixin_43407092/article/details/94179484