简体   繁体   中英

Why does my script keep looping over the files? Python

Hi essentially my script below works but then once its looped and counted over all the files it then keeps doing it again and again. Is there something weird in my code? I'm also using the dirs, files and, name variables in the code any help you could give would be great

for root, dirs, files in os.walk(input):
    for name in files:

        if "fvds" in root:
            count = 0
            for gz in glob.glob(path.join(root, "*.gz")):
                print "Processing", gz
                with gzip.open(gz) as gzfile:
                    count += len(gzfile.readlines())
                print "%i features read" % count
                text_file.write("%i features read" % count+"\n") 

you are opening your gzipped files in the the folder that you are walking over , this is creating another file for it to loop over when the files don't exist. This will trigger another iteration of os.walk as the new file arrives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM