Python OS. when the getcwd () function obtains the current path. whether to understand the prototype of the current path obtained by the getcwd () function. The following article analyzes the prototype and describes the content of the article.
Obtain the current path from the Python OS. getcwd () function. The prototype is as follows.
OS. getcwd ()
This function does not need to pass parameters. It returns the current directory. Note that the current directory is not the directory where the script is located, but the directory where the script is running. For example, input the following script in PythonWin.
- >>> import os
- >>> print 'current directory is ',os.getcwd()
- current directory is D:\Python25\Lib\site-packages\pythonwin
Here is the installation directory of PythonWin if the above content is written to pwd. py, assuming pwd. py is located in the E: \ book \ code directory. in the Windows command line window, enter the E: \ book directory and enter code \ pwd. py, the output is as follows.
- E:\book>code\pwd.py
- current directory is E:\book
Obtain contents in the directory
In Python, you can use the OS. listdir () function to obtain the content in the specified directory. The prototype is as follows.
- os.listdir(path)
The parameter meanings are as follows. · Path: the path to obtain the content directory. The following instance obtains the contents of the current directory.
- >>> import os
- >>> os.listdir(os.getcwd())
Obtain the content in the current directory
- ['dde.pyd', 'license.txt', 'Pythonwin.exe',
'scintilla.dll', 'win32ui.pyd', 'win32uiole.
pyd', 'pywin']
The above content is an introduction to Python file and directory operations and Python OS. getcwd () function to get the current path for practical application.