Detailed description of Python file-related practical application solutions and codes

Source: Internet
Author: User

The following articles describe the actual application solution of the Python file in the actual application process, and explain the code of the Python file, if you are interested in the practical application solutions related to Python files, you can read the following articles to better understand them.

Python File

 
 
  1. import os  
  2. import os.path 

OS and OS. path contain functions accessed by most files. Therefore, you must first introduce Python files. Please modify the path according to your actual situation.

 
 
  1. rootdir = ” d:/download ”  
  2. for parent, dirnames, filenames in os.walk(rootdir):  
  3. # case 1:  
  4. for dirname in dirnames:  
  5. print ( ” parent is: ” + parent)  
  6. print ( ” dirname is: ” + dirname)  
  7. # case 2  
  8. for filename in filenames:  
  9. print ( ” parent is: ” + parent)  
  10. print ( ” filename with full path : ” + os.path.join(parent, filename)) 

Knowledge point:

OS. walk returns a triple. where dirnames is the name of all folders (excluding paths), and filenames is the name of all files (excluding paths ). parent indicates the parent directory. case1 demonstrates how to traverse all directories. case2 demonstrates how to traverse all Python files.

 
 
  1. os.path.join(dirname,filename) : 

Convert "/a/B/c" and "d. java" to "/a/B/c/d. java ".

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.