Reproduced Python recursively iterates through all the files in the directory

Source: Internet
Author: User

#自定义函数:

Import OS
Path= "D:\\temp_del\\a"
def GCI (path):
"" "This is a statement" ""
Parents = Os.listdir (path)
For the parent in parents:
Child = Os.path.join (path,parent)
#print (Child)
If Os.path.isdir (child):
GCI (Child)
# Print (Child)
Else
Print (child)

GCI (PATH)

Print (gci.__doc__) #显示函数声明部分内容

def function_name (param):
"" "" "This is a statement." "" "" ""
You can use the function's property __doc__ to return the declaration, such as print (FUNCTION_NAME.__DOC__)

#使用os. Walk method Traversal:

Import OS
Path= "D:\\temp_del\\a"
For I in Os.walk (path):
Print (i)

return Result:

(' d:\\temp_del\\a ', [' AFA ', ' x64 '], [' Audiofilteragent.ini ', ' Setup.exe '])
(' D:\\temp_del\\a\\afa ', [' 222 '], [' CAudioFilterAgent.exe ', ' CAudioFilterAgent64.exe '])
(' d:\\temp_del\\a\\afa\\222 ', [], [' New journal document-copy. Jnt ', ' new journal document. Jnt '])
(' d:\\temp_del\\a\\x64 ', [' BBB '], [' Audiofilteragent.ini ', ' Setup64.exe '])
(' d:\\temp_del\\a\\x64\\bbb ', [], [' CAudioFilterAgent.exe ', ' CAudioFilterAgent64.exe '])

Return result Description:

The return is a ternary tupple (Dirpath, Dirnames, filenames),
The first one is the starting path, the second is the folder under the starting path, and the third is the file under the starting path.
Dirpath is a string that represents the path to the directory,
Dirnames is a list that contains the names of all subdirectories under Dirpath,
Filenames is a list that contains the name of a non-directory file. These names do not contain path information, and you need to use Os.path.join (Dirpath, name) if you need to get the full path.

=====================

Method One: Os.listdir

#!/usr/bin/python#-*-coding:utf-8-*-import osdef GCI (filepath): #遍历filepath下所有文件, including subdirectories  files = Os.listdir ( filepath) for  fi in Files:    fi_d = Os.path.join (filepath,fi)                if Os.path.isdir (fi_d):      GCI (fi_d)                      else:      print os.path.join (filepath,fi_d) #递归遍历 all files in the/root directory GCI ('/root ')

Method Two: Os.walk

#!/usr/bin/python#-*-coding:utf-8-*-import osfor fpathe,dirs,fs in Os.walk ('/root '): For  F in fs:    print Os.pat H.join (FPATHE,F)

List all Files:

[I for I in Os.listdir ('. ') if Os.path.isdir (i)]

List all. py files [i for I in Os.listdir ('. ') if Os.path.isfile (i) and Os.path.splitext (i) [1]== '. Py ']

Reproduced Python recursively iterates through all the files in the directory

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.