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
- import os
- 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.
- rootdir = ” d:/download ”
- for parent, dirnames, filenames in os.walk(rootdir):
- # case 1:
- for dirname in dirnames:
- print ( ” parent is: ” + parent)
- print ( ” dirname is: ” + dirname)
- # case 2
- for filename in filenames:
- print ( ” parent is: ” + parent)
- 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.
- os.path.join(dirname,filename) :
Convert "/a/B/c" and "d. java" to "/a/B/c/d. java ".