Get the App
SLTechnology News&Howtos  ›  Development  › 

The application scheme of Python file and how to write the code

Shulou Source: shulou.com Published: 2022-06-02 23:32:02 10月22日 Update

Python file related applications and how to write code, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Python file

Import os import os.path

Os,os.path contains most file access functions, so introduce Python files first. Please modify this 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 points:

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 represents the parent directory. Case1 demonstrates how to traverse all directories. Case2 demonstrates how to traverse all Python files.

Os.path.join (dirname,filename):

Change shapes like "/ a/b/c" and "d.java" to / a/b/c/d.java ".

After reading the above, have you mastered the relevant application scheme of Python file and the method of how to write the code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: File path code scheme application content name method more directory problem demonstration helpless for this function reason actual this situation skill Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Shulou Tech Info Huawei Xiaomi