在 Python 中使用 os.walk 时出现错误 2
Errno 2 while using os.walk in Python
这是一个搜索大于指定大小的文件的脚本:
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'
我猜是因为 Python 解释器无法扫描自身,所以我试图从搜索中跳过 'anaconda3' 文件夹(上面代码中用#skip anaconda 文件夹标记)。但是,错误消息仍然相同。
谁能解释一下?
(如果这里不允许此类问题,请让我知道,应该编辑。谢谢)
文件 python 正在尝试获取 os.stat(filename).st_size
的大小是损坏的 link。损坏的 link 是目标已被移除的 link。它很像 Internet link 给出 404。要在脚本中修复此问题,请检查它是否是文件(首选),或使用 try/catch(非首选)。要检查文件是否是文件而不是损坏的 link,请使用 os.path.isfile(file_path)
。您的代码应如下所示:
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')
因此,在获取大小之前,它会检查文件是否真的存在,并跟踪所有 link 以确保它存在。 .
这是一个搜索大于指定大小的文件的脚本:
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'
我猜是因为 Python 解释器无法扫描自身,所以我试图从搜索中跳过 'anaconda3' 文件夹(上面代码中用#skip anaconda 文件夹标记)。但是,错误消息仍然相同。
谁能解释一下?
(如果这里不允许此类问题,请让我知道,应该编辑。谢谢)
文件 python 正在尝试获取 os.stat(filename).st_size
的大小是损坏的 link。损坏的 link 是目标已被移除的 link。它很像 Internet link 给出 404。要在脚本中修复此问题,请检查它是否是文件(首选),或使用 try/catch(非首选)。要检查文件是否是文件而不是损坏的 link,请使用 os.path.isfile(file_path)
。您的代码应如下所示:
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')
因此,在获取大小之前,它会检查文件是否真的存在,并跟踪所有 link 以确保它存在。