简体   繁体   中英

Errno 2 while using os.walk in Python

This is a script searching for files that are bigger than a specified size:

def size_scan(folder, size=100000000):
    """Scan folder for files bigger than specified size

    folder: abspath
    size: size in bytes
    """
    flag = False

    for folder, subfolders, files in os.walk(folder):
        # skip 'anaconda3' folder
        if 'anaconda3' in folder:
            continue

        for file in files: 
            file_path = os.path.join(folder, file)
            if os.path.getsize(file_path) > size:
                print(file_path, ':', os.path.getsize(file_path))
                flag = True

    if not flag:
        print('There is nothing, Cleric')

I get the following error message while scanning root folder in Linux:

Traceback (most recent call last):

  File "<ipython-input-123-d2865b8a190c>", line 1, in <module>
    runfile('/home/ozramsay/Code/sizescan.py', wdir='/home/ozramsay/Code')

  File "/home/ozramsay/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "/home/ozramsay/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/ozramsay/Code/sizescan.py", line 32, in <module>
    size_scan('/')

  File "/home/ozramsay/Code/sizescan.py", line 25, in size_scan
    if os.path.getsize(file_path) > size:

  File "/home/ozramsay/anaconda3/lib/python3.6/genericpath.py", line 50, in getsize
    return os.stat(filename).st_size

FileNotFoundError: [Errno 2] No such file or directory: '/run/udev/link.dvdrw'

I guessed it is because Python interpreter can not scan itself, so I tried to skip 'anaconda3' folder from the search (marked by #skip anaconda folder in the code above). However, the error message remained the same.

Can anyone please explain?

(Please let me know if such kind of questions is not allowed here and should be edited. Thank you)

The file python is trying get the size of with os.stat(filename).st_size is a broken link. A broken link is a link that has had it's target removed. It is much like an internet link that gives a 404. To fix this in your script, check if it is a file (preferred), or use a try/catch (not preferred). To check if the file is a file and not a broken link, use os.path.isfile(file_path) . Your code should look like this:

def size_scan(folder, size=100000000):
"""Scan folder for files bigger than specified size

folder: abspath
size: size in bytes
"""
flag = False

for folder, subfolders, files in os.walk(folder):
    # skip 'anaconda3' folder
    if 'anaconda3' in folder:
        continue

    for file in files: 
        file_path = os.path.join(folder, file)
        if os.path.isfile(file_path) and (os.path.getsize(file_path) > size):
            print(file_path, ':', os.path.getsize(file_path))
            flag = True

if not flag:
    print('There is nothing, Cleric')

So before it gets the size, it checks if the file is really there, following all links to make sure it exists. Related SO post .

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