在Python中使用os.walk时出现错误2

在Python中使用os.walk时出现错误2,python,os.walk,Python,Os.walk,这是一个脚本,用于搜索大于指定大小的文件: 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):

这是一个脚本,用于搜索大于指定大小的文件:

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')
在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'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/home/ozramsay/Code/sizescan.py',wdir='/home/ozramsay/Code')
文件“/home/ozramsay/anaconda3/lib/python3.6/site packages/spyder/utils/site/sitecustomize.py”,第880行,在runfile中
execfile(文件名、命名空间)
文件“/home/ozramsay/anaconda3/lib/python3.6/site packages/spyder/utils/site/sitecustomize.py”,第102行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“/home/ozramsay/Code/sizescan.py”,第32行,在
扫描大小(“/”)
文件“/home/ozramsay/Code/sizescan.py”,第25行,大小扫描
如果os.path.getsize(文件路径)>大小:
文件“/home/ozramsay/anaconda3/lib/python3.6/genericpath.py”,第50行,在getsize中
返回os.stat(文件名).st\u大小
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/run/udev/link.dvdrw'
我猜这是因为Python解释器无法扫描自身,所以我尝试从搜索中跳过'anaconda3'文件夹(在上面的代码中标记为#skip anaconda folder)。但是,错误消息保持不变

谁能解释一下吗


(如果此处不允许此类问题,请告诉我是否应编辑此类问题。谢谢)

python正在使用os.stat(文件名)尝试获取的文件大小。st_size是一个断开的链接。断开的链接是已删除其目标的链接。这很像一个提供404的互联网链接。要在脚本中修复此问题,请检查它是否为文件(首选),或使用try/catch(非首选)。要检查文件是否为文件而不是断开的链接,请使用。您的代码应该如下所示:

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')

因此,在获取文件大小之前,它会检查文件是否确实存在,并跟踪所有链接以确保文件存在

也许你应该告诉我们你在哪个文件夹执行这个功能。看起来您正在一些特殊文件夹上运行它(正常情况下,
/run/udev/
也会被扫描吗?
/run/udev/link.dvdrw
与DVD驱动器有关(可能是符号链接?)。可能在传递路径到
getsize
zezollo之前运行,我在LinuxMint的根文件夹上执行这个函数:size\u scan('/')Patrick Haugh,你说得对,它已经工作了。非常感谢你!是的,很有效,谢谢。特别感谢您对这个问题的编辑!