3

I'm traversing a directory tree using Python 2.7.x, getting the file and directory sizes as it traverses. The problem I'm running into is that it is mistaking alias files for directories, and then throwing an error that there's "No Such File or Directory".

Code below:

def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size

for dirName, subdirList, fileList in os.walk(rootDir, topdown=False):

    dirSize = get_size(dirName) #this throws an error on alias files

    for fname in fileList:
              #do other things

I tried os.path.isdir() as well and that doesn't work. Further, I tried

return File.FSResolveAliasFile(path, True)[0].as_pathname()

But that doesn't seem to pick up all the alias files.

Any thoughts?

ballofpopculture
  • 741
  • 1
  • 9
  • 18
  • By aliases do you mean symbolic links? – shaktimaan Mar 07 '14 at 23:48
  • @warunsl I'm pretty sure he means the OS X specific Alias file that can be created using the "Make Alias" option in Finder. (https://en.wikipedia.org/wiki/Alias_%28Mac_OS%29) – Alexander O'Mara Mar 07 '14 at 23:54
  • @ballofpopculter You code is working perfectly in Python 2.7.5 on OS X 10.9.2, on both aliases and symbolic links. Aliases are considered files, and symbolic links are skipped. – Alexander O'Mara Mar 08 '14 at 00:10

2 Answers2

0

See my comment on https://stackoverflow.com/a/21197881/838253 for background.

The short answer is that you can't do this in Python. There was a library which resolved alias, but this relied on an obsolete Carbon library, and no longer works.

You can detect alias at the terminal.

It may be possible to distinguish because alias have extended attributes.

Community
  • 1
  • 1
Milliways
  • 1,265
  • 1
  • 12
  • 26
0

It seems there are three type of link in MacOSX.

  • Alias (This can be executed from right click menu "create alias")
  • Soft link
  • Hard link

Alias link seems to be specific on MacOSX.

And python 2.7 seems not to recognize Alias link as link.

To make sure, try this.

os.path.islink("./alias-you-created")
>>> False

So it is not treated as link.

IMO I think python 2.7 is not support MacOSX Alias as link. But I can't understand why MacOSX has such function, it's too complicated.

Here is related information I found.

Community
  • 1
  • 1
Kei Minagawa
  • 4,395
  • 3
  • 25
  • 43
  • The 1st link above is interesting, but incorrect. `Alias` MAY contain `inode`, but they are not documented. `inode` only work on a single partition, but `Alias` work across partitions or disks, so it must rely on something else. It also fails to explain why `Alias` are so big - often MB – Milliways Mar 08 '14 at 03:02
  • Aliases are big because they store several sizes of retina icons. Here is a link to the discussion: http://apple.stackexchange.com/questions/81671/why-are-the-alias-so-big-in-filesize-in-mountain-lion – Louis Cremen Apr 13 '17 at 05:35