Introduction to the practical application solution of the Python OS. getcwd () function

Source: Internet
Author: User

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.

 
 
  1. >>> import os  
  2. >>> print 'current directory is ',os.getcwd()  
  3. 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.

 
 
  1. E:\book>code\pwd.py  
  2. 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.

 
 
  1. 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.

 
 
  1. >>> import os  
  2. >>> os.listdir(os.getcwd())   

Obtain the content in the current directory

 
 
  1. ['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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.