Before introducing absolute and relative paths, let's understand what the current working directory is.
Every program running on your computer has a "current working directory" (or cwd). All file names or paths that do not start from the root folder are assumed to be in the current working directory.
Note that although the folder is the updated name of the directory, the current working directory (or current directory) is a standard term and there is no such thing as the current working folder.
import os os.getcwd() os.chdir('C:\\Windows\\System32') os.getcwd()
The output is:
'C:\\Windows\\Python'
'C:\\Windows\\System32'
It can be seen that the original current working path is 'C:\\Windows\\Python' (that is, the desktop). Through the os.chdir() function, it is changed to' C:\\Windows\\System32 '.
It should be noted that if the working directory modified by os.chdir () does not exist, the Python interpreter will report an error, for example:
os.chdir('C:\\error')
The output is:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
[ipython-input-14-c95b0db87686] in [module>]
----> 1 os.chdir('C:\\error')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\error'
After understanding the specific meaning of the current working directory, the following introduces the meaning and usage of absolute and relative paths.
There are two ways to specify the path of a file:
When using a relative path to indicate the location of a file, in addition to often using .\ to indicate the current directory .\ is also used to indicate the parent directory of the current directory.
The Python os.path module provides functions that can convert between absolute and relative paths, and check whether a given path is an absolute path, such as:
Try the functions mentioned above in an interactive environment:
os.getcwd() os.path.abspath('.') os.path.abspath('.\\Scripts') os.path.isabs('.') os.path.isabs(os.path.abspath('.')) os.path.relpath('C:\\Windows', 'C:\\') os.path.relpath('C:\\Windows', 'C:\\spam\\eggs') path = 'C:\\Windows\\System32\\calc.exe' os.path.basename(path) os.path.dirname(path)
The output is:
'C:\\Windows\\System32'
'C:\\Windows\\System32'
'C:\\Windows\\System32\\Scripts'
False
True
'Windows'
'..\\..\\Windows'
'calc,exe'
'C:\\Windows\\System32'
Note that because the reader's system files and folders may be different from mine, the reader does not have to follow the examples in this section completely and make appropriate adjustments to the code in this section according to their system environment.
In addition, if you need both the directory name and the base name of a path, you can call os.path.split() to get a tuple of these two strings, for example:
path = "C:\Windows\System32\AdvancedInstallers\cmiv2.dll" os.path.split(path)
The output is:
('C:\\Windows\\System32\\AdvancedInstallers', 'cmiv2.dll')
Note that you can call os.path.dirname() and os.path.basename() and put their return values in a tuple to get the same tuple. But using os.path.split() is definitely a good shortcut.
At the same time, if the provided path does not exist, many Python functions will crash and report an error, but fortunately the os.path module provides the following functions to detect whether a given path exists and whether it is a file or a folder:
Here are the results of trying these functions in an interactive environment:
os.path.exists('C:\\Windows') os.path.exists('C:\\Python') os.path.isdir('C:\\Windows\\System32') os.path.isfile('C:\\Windows\\System32') os.path.isdir('C:\\Windows\\System32\\add.exe') os.path.isfile('C:\Windows\System32\AdvancedInstallers\cmiv2.dll')
The output is:
True
False
True
False
False
True
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises