python - 为什么 os.path.getsize() 在 python 中抛出错误?

标签 python os.path

我试图打印所有文件列表的大小,但它抛出错误。 这是代码:

import os

def printSize(fileAbsPath) :
  print os.path.getsize(fileAbsPath)


for folderName, subfolders, filenames in os.walk('/Users/kris/Desktop'):
  for filename in filenames :
    try :
      printSize(os.path.abspath(filename))
    except Exception as err:
      print 'An exception happend : ' + str(err)

并且,错误是:

An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.DS_Store'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.gitignore'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.gitmodules'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.localized'
.
.
.
.
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/BLE Scanner.m4a'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/Screen Shot 2015-07-23 at 3.30.52 PM.png'

我不知道这是怎么回事。我认为这与“隐藏文件”有点相关,但事实并非如此。

最佳答案

试试这个:

  printSize(os.path.abspath(os.path.join(folderName, filename)))

关于python - 为什么 os.path.getsize() 在 python 中抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33852057/

相关文章:

python - 通过对像素求和来重新调整矩阵

python - 一次打开多个(未指定数量)文件并确保它们正确关闭

python正则表达式模式提取两个字符之间的值

python - 这个ODE怎么解呢?

python - Django 图像表单不保存图像

python-2.7 - 如何解决 Python 2.7 上缺少 `exist_ok` 的问题?

windows - Windows 上的 os.path.getsize 在复制时报告完整文件大小

python - 如何用python在windows中打开文件?

python - os.path.join 似乎对打开斜杠很敏感,为什么?

python - tar 文件无法打开 tgz