Obtaining the attributes and properties of a folder recursively.

V

venutaurus539

Hello all,
Is there any way to list out all the properties (name,
type, size) and attributes( Accesstime, mod time, archived or readonly
etc) of a folder and its contents recursively. Should I need ot go
inside each and every directory to list them? This has to be for
Windows.
I have to obtain the following attriubtes of all the files
recursively in a directory:

NAME\ FILE CREATION TIME \ MODIFICATION TIME \ ACCESS TIME \
ATTRIBUTES \ ACLs \ File SIze \

Thank you,
Venu Madhav
 
V

venutaurus539

Just some hints you might want to check:

os.walk

 >>> help(os.walk)

and os.stat

please see the example for os.walk - this
would work on every platform supported by
python.

for acls, a little more work would be needed
if you want something filesystem specific but
posix style information is included with stat()

Regards
Tino

 smime.p7s
4KViewDownload

Thanks for your suggestion. By the way the attachment which have added
has some unknown file extension. May I know how can I view it?
 
T

Tim Golden

Hello all,
Is there any way to list out all the properties (name,
type, size) and attributes( Accesstime, mod time, archived or readonly
etc) of a folder and its contents recursively. Should I need ot go
inside each and every directory to list them? This has to be for
Windows.
I have to obtain the following attriubtes of all the files
recursively in a directory:

NAME\ FILE CREATION TIME \ MODIFICATION TIME \ ACCESS TIME \
ATTRIBUTES \ ACLs \ File SIze \

If you feel like living dangerously, try my
in-progress-but-already-working Winsys package:

http://timgolden.me.uk/python/downloads/WinSys-0.3dev.win32.exe

and do the following:

<code>
from winsys import fs

for f in fs.flat ("c:/temp"):
f.dump ()

</code>

TJG
 
T

Tim Golden

Tino said:
Tim Golden wrote:
...
^ eeek!

Was the eeeek! for the space before the bracket
(which, for some unaccountable reason disturbs
some people)? Or for the idea of dumping data
out?

This is what it looks like for one (random) file:

<dump>
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC
Type "help", "copyright", "credits" or "license" for{
c:\temp\t.py
{
c:\temp\t.py
parts: [u'\\\\?\\c:\\', u'temp', u't.py']
root: \\?\c:\
dirname: \temp
path: \\?\c:\\temp
filename: t.py
parent: \\?\c:\temp
}
readable: True
id: 260191347677670224354611
n_links: 1
created_at: 2008-10-27 09:45:09
accessed_at: 2009-03-20 08:57:25
written_at: 2009-03-20 08:57:25
uncompressed_size: 1170
size: 1170
Attributes:
{
ARCHIVE => True
COMPRESSED => False
DIRECTORY => False
ENCRYPTED => False
HIDDEN => False
NORMAL => False
NOT_CONTENT_INDEXES => False
OFFLINE => False
READONLY => False
REPARSE_POINT => False
SPARSE_FILE => False
SYSTEM => False
TEMPORARY => False
VIRTUAL => False
}
Security:
control:
{
DACL_AUTO_INHERITED
SELF_RELATIVE
DACL_PRESENT
}
owner: VOUK\goldent
dacl:
{
inherited: True
{
trustee: VOUK\goldent
access: 00000000000011111000000011111111
type: ACCESS_ALLOWED
}
{
trustee: BUILTIN\Administrators
access: 00000000000011111000000011111111
type: ACCESS_ALLOWED
flags:
{
INHERITED
}
}
{
trustee: NT AUTHORITY\SYSTEM
access: 00000000000011111000000011111111
type: ACCESS_ALLOWED
flags:
{
INHERITED
}
}
{
trustee: VOUK\goldent
access: 00000000000011111000000011111111
type: ACCESS_ALLOWED
flags:
{
INHERITED
}
}
{
trustee: BUILTIN\Users
access: 00000000000010010000000001010100
type: ACCESS_ALLOWED
flags:
{
INHERITED
}
}
}
}</dump>


TJG
 
V

venutaurus539

Tino said:
Tim Golden wrote:
...
         ^ eeek!

Was the eeeek! for the space before the bracket
(which, for some unaccountable reason disturbs
some people)? Or for the idea of dumping data
out?

This is what it looks like for one (random) file:

<dump>
Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC
Type "help", "copyright", "credits" or "license" for>>> from winsys import fs
{
  c:\temp\t.py
  {
    c:\temp\t.py
    parts: [u'\\\\?\\c:\\', u'temp', u't.py']
    root: \\?\c:\
    dirname: \temp
    path: \\?\c:\\temp
    filename: t.py
    parent: \\?\c:\temp
  }
  readable: True
  id: 260191347677670224354611
  n_links: 1
  created_at: 2008-10-27 09:45:09
  accessed_at: 2009-03-20 08:57:25
  written_at: 2009-03-20 08:57:25
  uncompressed_size: 1170
  size: 1170
  Attributes:
  {
    ARCHIVE => True
    COMPRESSED => False
    DIRECTORY => False
    ENCRYPTED => False
    HIDDEN => False
    NORMAL => False
    NOT_CONTENT_INDEXES => False
    OFFLINE => False
    READONLY => False
    REPARSE_POINT => False
    SPARSE_FILE => False
    SYSTEM => False
    TEMPORARY => False
    VIRTUAL => False
  }
  Security:
  control:
  {
    DACL_AUTO_INHERITED
    SELF_RELATIVE
    DACL_PRESENT
  }
  owner: VOUK\goldent
  dacl:
  {
    inherited: True
    {
      trustee: VOUK\goldent
      access: 00000000000011111000000011111111
      type: ACCESS_ALLOWED
    }
    {
      trustee: BUILTIN\Administrators
      access: 00000000000011111000000011111111
      type: ACCESS_ALLOWED
      flags:
      {
        INHERITED
      }
    }
    {
      trustee: NT AUTHORITY\SYSTEM
      access: 00000000000011111000000011111111
      type: ACCESS_ALLOWED
      flags:
      {
        INHERITED
      }
    }
    {
      trustee: VOUK\goldent
      access: 00000000000011111000000011111111
      type: ACCESS_ALLOWED
      flags:
      {
        INHERITED
      }
    }
    {
      trustee: BUILTIN\Users
      access: 00000000000010010000000001010100
      type: ACCESS_ALLOWED
      flags:
      {
        INHERITED
      }
    }
  }}

</dump>

TJG

Thank you for your suggestion but.. I'll have around 1000 such files
in the whole directory and it becomes hard to manage such output
because again I've to take this snapshot before backing up the data
and have to do the same and compare both when the data gets restored,
just to verify whether the restore happened successfully or not.
Thank you,
Venu Madhav
 
T

Tim Golden

Thank you for your suggestion but.. I'll have around 1000 such files
in the whole directory and it becomes hard to manage such output
because again I've to take this snapshot before backing up the data
and have to do the same and compare both when the data gets restored,
just to verify whether the restore happened successfully or not.


The .dump is simply a way of seeing quickly what the data is.
If you want to store is somewhere (.csv or whatever), you can
just select the attributes you want. Note, though, that ACLs
are tricky to compare. Something like the following might do
what you want, perhaps?

<code>
import os
import csv
from winsys import fs

with open ("info.csv", "wb") as f:
writer = csv.writer (f)
for f in fs.flat ("c:/temp/cabsdk"):
print f
writer.writerow ([
f,
f.created_at,
f.written_at,
f.attributes,
f.security (),
f.size
])

os.startfile ("info.csv")
</code>

This uses the security's SDDL representation, which while
opaque does at least give you a before-and-after check.

TJG
 
V

venutaurus539

Thank you for your suggestion but.. I'll have around 1000 such files
in the whole directory and it becomes hard to manage such output
because again I've to take this snapshot before backing up the data
and have to do the same and compare both when the data gets restored,
just to verify whether the restore happened successfully or not.

The .dump is simply a way of seeing quickly what the data is.
If you want to store is somewhere (.csv or whatever), you can
just select the attributes you want. Note, though, that ACLs
are tricky to compare. Something like the following might do
what you want, perhaps?

<code>
import os
import csv
from winsys import fs

with open ("info.csv", "wb") as f:
  writer = csv.writer (f)
  for f in fs.flat ("c:/temp/cabsdk"):
    print f
    writer.writerow ([
      f,
      f.created_at,
      f.written_at,
      f.attributes,
      f.security (),
      f.size
    ])

os.startfile ("info.csv")
</code>

This uses the security's SDDL representation, which while
opaque does at least give you a before-and-after check.

TJG

Thank you Sir for your reply. It is working for me. But is failing if
I have Unicode characters in my path. I tried giving a 'u' in front of
the path but still it fails at f.createdat. Does it support Unicode
Characters?

This the traceback which I got while running the above python script
on my data:
-----------------------------------
\\?\C:\JPDump\AGIT_FSDM\Unicode\Arabic files\Arabicְדזךמאּשּׁתּ
ï­„ï­Žï­‰ï­ï¬»ï¬¸ï¬ºï¬¯ï¬¬ï­Œï­Ž×°×²×´×˜×¦×ª×
Traceback (most recent call last):
File "C:\MFDump\snapshot.py", line 10, in <module>
f.created_at,
File "C:\Python26\Lib\site-packages\winsys\fs.py", line 476, in
_get_created_at
return utils.from_pytime (wrapped (win32file.GetFileAttributesEx,
self._filepath)[1])
File "C:\Python26\Lib\site-packages\winsys\exceptions.py", line 26,
in _wrapped
raise exception (errmsg, errctx, errno)
x_invalid_name: ('The filename, directory name, or volume label syntax
is incorrect.', 'GetFileAttributesEx', 123)

-----------------------------------------------------------------



Thank you,
Venu Madhav,
 
T

Tim Golden

Thank you for your suggestion but.. I'll have around 1000 such files
in the whole directory and it becomes hard to manage such output
because again I've to take this snapshot before backing up the data
and have to do the same and compare both when the data gets restored,
just to verify whether the restore happened successfully or not.
The .dump is simply a way of seeing quickly what the data is.
If you want to store is somewhere (.csv or whatever), you can
just select the attributes you want. Note, though, that ACLs
are tricky to compare. Something like the following might do
what you want, perhaps?

<code>
import os
import csv
from winsys import fs

with open ("info.csv", "wb") as f:
writer = csv.writer (f)
for f in fs.flat ("c:/temp/cabsdk"):
print f
writer.writerow ([
f,
f.created_at,
f.written_at,
f.attributes,
f.security (),
f.size
])

os.startfile ("info.csv")
</code>

This uses the security's SDDL representation, which while
opaque does at least give you a before-and-after check.

TJG

Thank you Sir for your reply. It is working for me. But is failing if
I have Unicode characters in my path. I tried giving a 'u' in front of
the path but still it fails at f.createdat. Does it support Unicode
Characters?

It certainly *should*: one of the design intentions is to
support unicode throughout (as well as trying to be robust
in the face of errors). I'll try to look at that later
today.

You might ultimately decide that you didn't want an
in-development package as part of your app, but at
least you can look at the code inside to see how it
does what it does -- none of which is very sophisticated --
and use what you need for your own project.

TJG
 
T

Tim Golden

Thank you Sir for your reply. It is working for me. But is failing if
I have Unicode characters in my path. I tried giving a 'u' in front of
the path but still it fails at f.createdat. Does it support Unicode
Characters?

This the traceback which I got while running the above python script
on my data:

In fact, to give me something to test against, would you
be able to send me the result of running this code, please?
It's just listing the names of the files under that particular
directly, encoded as utf8 so they'll survive transit:

<code>
import os
from winsys import fs

PATH = r"\\?\C:\JPDump\AGIT_FSDM\Unicode\Arabic files"
f = open ("arabic-files.txt", "wb")
for filename in fs.listdir (PATH):
f.write (filename.encode ("utf8") + "\r\n")

f.close ()
os.startfile ("arabic-files.txt")

</code>


Thanks
TJG
 
T

Tim Golden

This is the output for your code Sir. But I think os.stat is
sufficient to satisfy all my requirements. May be we just have to make
the path itself a Unicode path.

Arabicְדזךמ×ּשּ×תּפּפֿשּï­×›Ö¼×˜Ö¼×šÖ¼×ָשּ×בֿפֿװײ״טצת×
Arabicְדזךמ×ּשּ×תּפּפֿשּï­×›Ö¼×˜Ö¼×šÖ¼×ָשּ×בֿפֿװײ״טצת×.doc
Arabicְדזךמ×ּשּ×תּפּפֿשּï­×›Ö¼×˜Ö¼×šÖ¼×ָשּ×בֿפֿװײ״טצת×new.doc
الأبجدي.txt
تعلم اللغة الإيطالية مجان.doc
دروس مجانية ÙÙŠ اللغة الإنجليزي.txt
دروس مجانية ÙÙŠ اللغة الإنجليزي1.txt


Yes, thanks to your question I discovered one or two small
bugs in the WinSys unicode handling, hopefully now fixed.
But, I agree, you can use os.stat to do pretty much all
you want by passing unicode filenames. You'll also need
to use the win32security functions to get the ACL stuff.

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top