Python captures all files in a folder,
The examples in this article share the code for capturing all files in a folder in python for your reference. The details are as follows:
#! /User/bin/python #-*-coding: utf8-*-import Basic import OS ################################ ############################ Input #### folder = "D: \ installation package "folder = folder. encode ("gbk ") ######## Global #### fileList = [] allFiles = [] tree = [] level = 0 ############ ######################################## # try: if folder is None or len (folder) = 0: folder = OS. curdir if OS. path. isdir (folder): childFile = OS. listdir (folder) fileList = ["% s" % (folder + OS. path. sep + f) for f in childFile] node = {'file': folder, 'level': level} tree. append (node) while fileList is not None and len (fileList)> 0: allFiles. append ("" + fileList [0]) if OS. path. isdir (fileList [0]): childFile = OS. listdir (fileList [0]) if childFile is not None and len (childFile)> 0: fileList = fileList + ["% s" % (fileList [0] + OS. path. sep + ft) for ft in childFile] else: pass fileList. pop (0) print "\ n ". join (["% s" % f for f in allFiles]) else: print 'not folder, no child' failed t Exception, x: print x
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.