Remove the list of missing items in python check

Source: Internet
Author: User
Tags python list

Remove the list of missing items in python check

 

Today, I encountered a problem with the python list. Errors often occur in the areas you are most familiar with, especially in small areas. The problem is as follows: I want to list all the lower-level files and subdirectories under the directory (only including the lower-level directories but not the directories and files), and then retrieve only the current subdirectory .. The problem is clearly described and a function is easily written, not the OS. listdir (path), right. This function can list all directories and files at the next level, and the returned results are in a list, so the general idea of our main work is to filter this result list? Well, the OS also has related functions. path. isfile (filepath) can be used to determine whether a file is a file. If it is a file, delete it from the list. After processing, it is the final result list we want. Is that true? The idea is perfect. The reality is cruel and unexpected. This cannot be done. We use code to experiment:

 

 

import osdef Count_SubDir(src):    listall = os.listdir(src)    for line in listall:        filepath = os.path.join(src,line)        if os.path.isfile(filepath):            listall.remove(line)    return listall    if __name__ == "__main__":    src = r'f:\src'    ret = Count_SubDir(src)    for line in ret:        print line
Output result:

 

 

>>> ================================ RESTART ================================>>> 1025102810291030103110321033103510361037103810401041104210431044104510461049105310552052207030763082GraphicsNDP40-KB2894842.mspSetup.exeSetupUi.dllSetupUtility.exesqmapi.dllUiInfo.xml>>> 
Look at the end... There are still files. We will compare the list of original results that have not been processed:

 

 

>>> ================================ RESTART ================================>>> 1025102810291030103110321033103510361037103810401041104210431044104510461049105310552052207030763082DHtmlHeader.htmlGraphicsheader.bmpNDP40-KB2894842.mspParameterInfo.xmlSetup.exeSetupEngine.dllSetupUi.dllSetupUi.xsdSetupUtility.exeSplashScreen.bmpsqmapi.dllStrings.xmlUiInfo.xmlwatermark.bmp>>> 
The final file project can be compared to see if any rule is found... Missed one by one...

 

Why is this happening? This is because there is a problem with the combination of the for loop and the list. remove () method. After you go through the process, the problem is exposed.

Let's go through it again: first, for loop, select a line for each loop. If this line is a file, delete it from the list, and then the second loop .... The question is, is there anything missing? After deletion, is there any operation without code display? The answer is,

 

def Count_SubDir(src):    listdir = []    listall = os.listdir(src)    for line in listall:        filepath = os.path.join(src,line)        if os.path.isdir(filepath):            listdir.append(filepath)    return listdir
Output result:

 

 

Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> f:\src\1025f:\src\1028f:\src\1029f:\src\1030f:\src\1031f:\src\1032f:\src\1033f:\src\1035f:\src\1036f:\src\1037f:\src\1038f:\src\1040f:\src\1041f:\src\1042f:\src\1043f:\src\1044f:\src\1045f:\src\1046f:\src\1049f:\src\1053f:\src\1055f:\src\2052f:\src\2070f:\src\3076f:\src\3082f:\src\Graphics>>> 

Okay, this time it's really complete. It's a real directory .... I can't believe it. When I couldn't find a problem at first, I always felt okay... If the result is not analyzed, the end of the remove Method is missing... I think many people only know the delete step of remove. I didn't think of the List adjustment after deletion. Haha!



 

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.