Reading the access attributes of directories in Windows

Tim Golden mail at timgolden.me.uk
Mon Aug 23 16:07:03 CEST 2010


On 23/08/2010 14:55, vsoler wrote:
> On Aug 21, 8:10 am, Tim Golden<m... at timgolden.me.uk>  wrote:
>> On 20/08/2010 11:54 PM, vsoler wrote:
>>
>>> I'am testing your library. I am mainly interested in knowing the
>>> access attributes of directories in the local(C:\) or shared unit(W:\)
>>> of my system.
>>
>>> Using your script with 'c:\\' I get an error message saying... 'file
>>> exists but it is a directory' and I cannot go any further.
>>
>>> Of course, the problem is that I am using "fs.file" when I should be
>>> using something different.
>>
>> Either use fs.dir (if you know it's a directory) or fs.entry (if it
>> could be a file or a directory; the code will dispatch to the right one).
>>
>> If you only want the directories immediately some directory,
>> you could do this:
>>
>> <code>
>> from winsys import fs, security
>>
>> root = fs.file (sys.executable).path  # or fs.dir ("w:/") etc.
>> for d in root.dirs (ignore_access_errors=True):
>>     print (d, "=>", d.security ()) # or whatever
>>
>> </code>
>>
>> If you want to walk the tree of directories looking at permissions, then:
>>
>> <code>
>> import os, sys
>> from winsys import fs
>>
>> root = fs.file (sys.executable).path
>> for dirpath, _, _ in root.walk ():
>>     print (dirpath, "=>", dirpath.security ())
>>
>> </code>
>>
>>> Reading the doc I have found that I should be using os.walk(...),
>>> which works, but then I cannot use fs.file
>>
>> In fact, even if you did for some reason use os.walk, you can
>> easily wrap the returned filenames using fs.entry:
>>
>> <code>
>> import os, sys
>> from winsys import fs
>>
>> root = os.path.dirname (sys.executable)
>> for dirpath, filenames, dirnames in os.walk (root):
>>     print (dirpath, "=>", fs.entry (dirpath).security ())
>>
>> </code>
>>
>> TKG
>
> Tim,
>
> One of your scripts still does not work on my system:
>
>
> ==>  If you want to walk the tree of directories looking at
> permissions, then:
>
> <code>
> import os, sys
> from winsys import fs
>
> root = fs.file (sys.executable).path
> for dirpath, _, _ in root.walk ():
>     print (dirpath, "=>", dirpath.security ())
> </code>
>
> However, I get the following error:
>
> Traceback (most recent call last):
>    File "C:/Local/test4.py", line 5, in<module>
>      root = fs.file (r'W:\FRIB\ELPR\$DATA\DPT-FINANZAS').path
>    File "C:\Program Files\Python31\lib\site-packages\winsys\fs.py",
> line 1775, in file
>      raise x_fs (None, "file", "%s exists but is a directory" %
> filepath)
> winsys.fs.x_fs: (None, 'file', 'W:\\FRIB\\ELPR\\$DATA\\DPT-FINANZAS
> exists but is a directory')
>
> That is, I am interested in looking for directories, but the problem
> is that the path is a directory.
>
> I'm sure there must be some way to get around this problem.


Replace fs.file by fs.entry: the latter detects automatically whether
it's looking at a file or at a directory. I used fs.file in my example
because I *know&* what sys.executable must be a file (python.exe). In
your case, either use fs.dir if you know it's a directory or fs.entry
if it could be either. (Obviously fs.entry must do some work to determine
which it is, so you can optimise slightly by specifying fs.dir / fs.file)

TJG



More information about the Python-list mailing list