0

I am writing a python script to give me the sizes of my directory and subdirectories. I am getting the following error.

=======================
        AMD
                Bytes: 23917297664
                Megs: 22809.3125
                Gigs: 22.27471923828125
Traceback (most recent call last):
  File "c:\users\user\desktop\temp3.py", line 13, in <module>
    sum(os.path.getsize(f) for f in os.listdir(dir) if os.path.isfile(f))
PermissionError: [WinError 5] Access is denied: 'Documents and Settings

Here is my code

import os


dir_size = sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f))
print(os.getcwd())
print(os.listdir('.'))
print("\tBytes: {}".format(dir_size))
print("\tMegs: {}".format(dir_size/1048576))
print("\tGigs: {}".format((dir_size/1048576)/1024))

dirs = os.listdir('.')

for dir in dirs:
    if "$" not in dir:
        sum(os.path.getsize(f) for f in os.listdir(dir) if os.path.isfile(f))
        print("=======================")
        print("\t{}".format(dir))
        print("\t\tBytes: {}".format(dir_size))
        print("\t\tMegs: {}".format(dir_size/1048576))
        print("\t\tGigs: {}".format((dir_size/1048576)/1024))

I am the only user on the system, I am the Administrator so I thought I should of course have permissions to see everything. Any ideas?

As a side note: PermissionError: [WinError 5] Access is denied python using moviepy to write gif has been suggested as a similar solution/question, below I explain why this is not the case.

Not the same, the solutions provided do not apply here. I am admin, I am running it as admin, and I am not using the moviepy lib

mikeg
  • 444
  • 3
  • 13
  • Possible duplicate of [PermissionError: \[WinError 5\] Access is denied python using moviepy to write gif](https://stackoverflow.com/questions/26091530/permissionerror-winerror-5-access-is-denied-python-using-moviepy-to-write-gif) – keepAlive Jan 04 '19 at 16:21
  • Thanks, I looked at that when I posted this. Not the same, the solutions provided do not apply here. I am admin, I am running it as admin, and I am not using the moviepy lib, but thanks. – mikeg Jan 04 '19 at 16:24
  • How do you run it? Via IDLE? – keepAlive Jan 04 '19 at 16:30
  • I run via the command line in Windows 10. – mikeg Jan 04 '19 at 16:37
  • not sure, but instead of `os.listdir(dir)` try : `os.listdir(os.path.join(dirs,dir))` – Shihab Shahriar Khan Jan 04 '19 at 16:43
  • Ill give it a try, thanks – mikeg Jan 04 '19 at 16:44
  • Well, that gave me sum(os.path.getsize(f) for f in os.listdir(os.path.join(dirs,dir)) if os.path.isfile(f)) File "D:\Python36\lib\ntpath.py", line 76, in join path = os.fspath(path) TypeError: expected str, bytes or os.PathLike object, not list – mikeg Jan 04 '19 at 16:51
  • Try this if it works: https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory – nakul powrakutsa Jan 04 '19 at 16:57
  • the problem is not finding the directory, it is with reading the contents – mikeg Jan 04 '19 at 17:01

1 Answers1

2

In Windows, the Administrator doesn't have access to everything right away.
It is more complicating.

See: Windows 7 Access Denied For Administrator:

The easiest [way] is to turn UAC off. Folder access will then behave exactly like XP

and:

With UAC enabled, to access a folder you need to run Windows Explorer [...] from the start menu, right clicking it and choose “Run as Administrator” [...]
unfortunately this doesn’t work due to a bug in Windows Explorer

(But I suggest to check if a workaround would be good enough, and if not be sure that you understand exactly what you are doing.)

See also: Access denied even though I'm an Administrator?

Workarounds:

1)

You can catch the error:
(of course, this way you would just accept to not having access)

import os

def getList( dir ):
    try:
        return os.listdir( dir )
    except Exception as e:
        print('!ERROR: {}, {}'.format( type(e).__name__, dir ) )
    return []

root = 'c:/'
for dir in getList( root ):
    dir = root + '/' + dir
    if os.path.isdir( dir ):
        dir_size = sum( os.path.getsize( dir + '/' + f ) for f in getList( dir ) if os.path.isfile( dir + '/' + f ) )
        print( "{}, {}".format( dir_size, dir ) )

2)

You can use os.walk():

os.walk() will work without an error, but:

  • always traverses all files / folders in all levels.
  • doesn't show C:\Documents and Settings at all
    I don't know for sure what criteria are responsible for that. Obviously 'permission denied' folders are not shown (?) (Own junctions and hidden files are shown.)

You can ignore levels (but all will be traversed in the background anyway):

import os

path = 'C:'
startlevel = path.count( os.sep )
level = 0
w = os.walk( path )

for p, dn, fn in w:
    level = p.count( os.sep )
    if level - startlevel < 1:
        print( level, p )

print('done.')

Junctions:

E.g. C:\Documents and Settings is a junction to C:\Users in in Windows 7.

You can't access C:\Documents and Settings,
but you can access C:\Documents and Settings\yourUserName = c:/Users/yourUserName.

d = os.listdir('c:/Documents and Settings')               # -> ERROR
d = os.listdir('c:/Documents and Settings/yourUserName')  # -> OK

os.listdir('c:/Documents and Settings/yourUserName') == os.listdir('c:/Users/yourUserName') # -> True

There is no easy way to check for windows junctions in Python 2 and 3.
os.path.islink and pathlib.Path().is_symlink() don't work for windows junctions:

os.path.islink('C:/Documents and Settings')              # returns False
pathlib.Path('C:/Documents and Settings').is_symlink()   # returns False

There is an answer that seems discuss windows junctions in python (that goes beyond my knowledge): implementing a readlink() function

See also Check if file is symlink in python.

kca
  • 4,856
  • 1
  • 20
  • 41
  • Junctions are used for mountpoints. When they target volume GUID names (as mountvol.exe does), they're similar to Unix mounts. When they target local paths (as CMD's `mklink /j`), they're similar to Unix bind mounts. "\Documents and Settings" is a bind-style junction to "\Users" that exists for backwards compatibility. The security on this junction denies Everyone the right to list its contents, since it only exists to be traversed. It's strictly an access issue due to the security on the directory; otherwise there's nothing special about junctions in this regard. – Eryk Sun Jan 07 '19 at 09:20