YouTip LogoYouTip

Python Os Path

## Python2.x Python os.path Module [![Image 1: Python File(Files) Method](#) Python OS Files/Directory Method](#) * * * The os.path module is mainly used to obtain file properties. The following are some common methods of the os.path module: | Method | Description | | --- | --- | | os.path.abspath(path) | Return absolute path | | os.path.basename(path) | Return filename | | os.path.commonprefix(list) | Return the longest common path prefix in list (multiple paths) | | os.path.dirname(path) | Return file path | | os.path.exists(path) | Return True if path exists; Return False if path does not exist or is corrupted. | | os.path.lexists(path) | Return True if path exists, also return True if path is corrupted | | os.path.expanduser(path) | Convert ~ and ~user in path to user directory | | os.path.expandvars(path) | Replace $name and ${name} in path according to environment variable values | | os.path.getatime(path) | Return last access time (floating point seconds) | | os.path.getmtime(path) | Return last file modification time | | os.path.getctime(path) | Return file creation time | | os.path.getsize(path) | Return file size, return error if file does not exist | | os.path.isabs(path) | Check if path is absolute | | os.path.isfile(path) | Check if path is a file | | os.path.isdir(path) | Check if path is a directory | | os.path.islink(path) | Check if path is a link | | os.path.ismount(path) | Check if path is a mount point | | os.path.join(path1[, path2[, ...]]) | Join directory and filename into a path | | os.path.normcase(path) | Convert path's case and slashes | | os.path.normpath(path) | Normalize path string form | | os.path.realpath(path) | Return the real path of path | | os.path.relpath(path[, start]) | Calculate relative path starting from start | | os.path.samefile(path1, path2) | Check if directories or files are the same | | os.path.sameopenfile(fp1, fp2) | Check if fp1 and fp2 point to the same file | | os.path.samestat(stat1, stat2) | Check if stat tuples stat1 and stat2 point to the same file | | os.path.split(path) | Split path into dirname and basename, return a tuple | | os.path.splitdrive(path) | Generally used on windows, return a tuple of drive name and path | | os.path.splitext(path) | Split path, return a tuple of path name and file extension | | os.path.splitunc(path) | Split path into mount point and file | | os.path.walk(path, visit, arg) | Traverse path, call visit function for each directory entered, visit function must have 3 parameters (arg, dirname, names), dirname represents the current directory name, names represents all file names in the current directory, args is the third parameter of walk | | os.path.supports_unicode_filenames | Set whether to support unicode filenames | ### Example The following example demonstrates the use of os.path related methods: ## Example import os print(os.path.basename('/root/tutorial.txt'))print(os.path.dirname('/root/tutorial.txt'))print(os.path.split('/root/tutorial.txt'))print(os.path.join('root','test','tutorial.txt')) Executing the above program outputs: tutorial.txt /root ('/root', 'tutorial.txt') root/test/tutorial.txt The following example outputs file related information. ## Example import os import time file='/root/tutorial.txt'print(os.path.getatime(file))print(os.path.getctime(file))print(os.path.getmtime(file))print(time.gmtime(os.path.getmtime(file)))print(os.path.getsize(file))print(os.path.abspath(file))print(os.path.normpath(file)) Executing the above program outputs: 1539052805.57357361539052805.57757351539052805.5735736 time.struct_time(tm_year
← Numpy Array From Numerical RanNumpy Install β†’