Automate the boring Stuff study notes 01
The walk () function of the OS module allows you to implement the operation of the catalog, which takes an absolute path string as a required parameter and returns three parameters:
- Current directory-the current working directory of the program--name (string format)
- Current directory-the directory that the program is currently in--all sub-folders (list format)
- Current directory----All Files (list format) under which the program is currently being searched
Suppose you have the following file structure:
The program code is as follows:
Import OS
For FolderName, subfolders, filenames in Os.walk (' c:\\delicious '):
Print (' The current folder is ' + folderName)
For subfolder in subfolders:
Print (' subfolder of ' + FolderName + ': ' + subfolder)
For filename in filenames:
Print (' FILE INSIDE ' + folderName + ': ' + filename ')
Print (")
The program output results are:
The current folder is C:\delicious
subfolder of C:\delicious:cats
subfolder of C:\delicious:walnut
FILE INSIDE C:\delicious:spam.txt
The current folder is C:\delicious\cats
FILE INSIDE C:\delicious\cats:catnames.txt
FILE INSIDE c:\delicious\cats:zophie.jpg
The current folder is C:\delicious\walnut
subfolder of C:\delicious\walnut:waffles
The current folder is C:\delicious\walnut\waffles
FILE INSIDE C:\delicious\walnut\waffles:butter.txt.
Python Calendar Catalog