Document directory
- OS. Path. abspath (relative_path)
- OS. Path. relpath (path [, start])
- OS. Path. basename (PATH)
- OS. Path. dirname (PATH)
- OS. Path. Split (PATH)
- OS. Path. Join (path1 [, path2 [,...])
- OS. Path. normpath (PATH)
- OS. Path. exists (PATH)
- OS. Path. Walk (path, callback, ARG)
OS. Path. abspath (relative_path)
Returns the absolute path.
Abspath = normpath (join (OS. getcwd (), relative_path ))
Sample:
The current path is/home/justtest, then:
abspath('./code/test.py') => '/home/justtest/code/test.py
OS. Path. relpath (path [, start])
Returns the relative path. The default value of start IS OS. curdir.
Assume that the current path is/home/justtest.
relpath('/home/justtest/test.py') => 'test.py'relpath('/home/anothertest/test.py', '/home/anothertest') => 'test.py'
OS. Path. basename (PATH)
Returned file name
Basname (PATH) = Split (PATH) [1]
basename('/home/justtest/test.py') =>'test.py'basename('/home/justtest/') =>''
OS. Path. dirname (PATH)
The returned directory name, excluding the file name. Note: The returned path name does not contain the last slash.
Dirname (PATH) = Split (PATH) [0]
dirname('/home/justtest/test.py') =>/home/justtest'dirname('/home/justtest/') =>'/home/justtest'
OS. Path. Split (PATH)
Break the path into (path, file name)
split('/home/justtest/test.py') = ('/home/justtest', 'test.py')split('/home/justtest/') = ('/home/justtest', '')
OS. Path. Join (path1 [, path2 [,...])
Merge multiple paths
join('/home', 'justtest', 'test.py') => '/home/justtest/test.py'join('/home/justtest', 'test.py') => '/home/justtest/test.py'
OS. Path. normpath (PATH)
Normalize paths: Remove unnecessary delimiters, convert "..." to "True paths", and handle incorrect slashes.
normpath('\home/justtest') => '\\home/justtest'normpath('/home/./justtest') => '/home/justtest'normpath('/home/../justtest') => '/justtest'
OS. Path. exists (PATH)
Whether the file or path exists and has the permission to access it
OS. Path. isabs (PATH), isfile (PATH), isdir (PATH), islink (PATH)
Isabs: whether the path is absolute
Isfile: whether to file
Isdir: path?
Islink: whether to link
OS. Path. Walk (path, callback, ARG)
Traverses the path and calls the callback function for each file in the path.
The following is a prototype of the callback function:
Callback (ARG, path, files) @ Arg: parameter of the walk function @ path: path @ files: All files in the path
The following code prints all file names in the current directory:
from os.path import *def walk_callback(arg, path, files):for file in files:print join(path, file), argwalk('.', walk_callback, 'hello from walk')