Home > Net >  os.walk finds files that don't exist
os.walk finds files that don't exist

Time:06-25

I was creating a program that checked for files in my Downloads folder and moved them in some subfolders depending on their extention.

I used os.walk("path") # path is the path to my downloads folder and printed the files it found, just to see if it worked.

The problem is that the program finds the files in the folder, but it also finds some files that aren't there, most of them end with .zpa, and it also finds a desktop.ini file.

Is this normal or is there something wrong?

CodePudding user response:

os.walk will find all files including those marked with hidden and system attributes. Those files are not displayed by default in the Windows Explorer or the command line dir utility.

If you want to skip hidden and system files in Python you'll have to check for the file attributes:

import os
import stat

for path,dirs,files in os.walk('Downloads'):
    for file in files:
        filename = os.path.join(path,file)
        info = os.stat(filename)
        if not info.st_file_attributes & (stat.FILE_ATTRIBUTE_SYSTEM | stat.FILE_ATTRIBUTE_HIDDEN):
            print(filename)

CodePudding user response:

See os.walk finds all files inside a directory. Those files which are hidden in Windows file explorer are also found out by this. Hidden files can include

  • Files hidden by you from within file explorer
  • Files hidden by OS itself, which are OS specific configuration files which we don't have to worry much about.
  • Files hidden by any application software intentionally

Probably there can be files from the above category which you got to see in the output.

As far as .zpa files are concerned, I don't know about those, but found a link which could help you.

  • Related