0

I am learning python atm and in order to do something useful whilst learning, I have created a small plan:

  1. Read specific disc drive partition. Outcome: List of directories
  2. Iterate each file within directory and subdirectories. Outcome: List of files within directories
  3. Read file information: extension Outcome: File extension
  4. Read file information: size Outcome: Size
  5. Read file information: date created Outcome: Date
  6. Read file information: date modified Date Read file information: owner Outcome:Ownership

At step 1 I have tried several approaches, scandir:

import os as os
x = [f.name for f in os.scandir('my_path') if f.is_file()]
with open('write_to_file_path', 'w') as f:
    for row in x:
        print(row)
        f.write("%s\n" % str(row))
f.close()

and this:

import os as os
rootDir = ('/Users/Ivan/Desktop/revit dynamo/')
for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
for fname in fileList:
    print('\t%s' % fname)

Though I have hard time writing a result into txt file.

May I ask what would be an ideal approach to make an audit of the specific directories with all relevant information extracted and stored as a table in txt file for now?

P.S.: my first question here, so please do not judge to strictly :)

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
Ivan Baranov
  • 1
  • 1
  • 3
  • 1
    There are many similar questions https://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python, https://stackoverflow.com/questions/1830618/how-to-find-the-owner-of-a-file-or-directory-in-python, for extension os.path.splitext – Joe Aug 28 '17 at 10:50
  • For writng to a file use e.g. the `csv` module – Joe Aug 28 '17 at 10:52
  • Thank you Joe.How would I, for instance, iterate through every single file and get creation date, etc ? – Ivan Baranov Aug 28 '17 at 10:56
  • You just use the list that `os.walk` or `glob` (https://pymotw.com/2/glob/) returns and iterate through it and pass the file name to the functions I mentioned above. – Joe Aug 28 '17 at 13:00

1 Answers1

1

Since you are learning Python3, I would suggest as an alternative to the low-level path manipulation using os.path, you could try pathlib(part of standard library as of Python 3.4):

from pathlib import Path
p = Path(mydir)

#list mydir content
for child in p.iterdir(): 
    print(child)

#Recursive iteration 
for child in p.glob("**/*"):
    if child.is_dir():
        #do dir stuff
    else:
        print(child.suffix)  # extension
        print(child.owner()) # file owner

        child_info = child.stat()
        #file size, mod time
        print(child_info.size,child_info.st_mtime)

File creation time is platform-dependent, but this post presents some solutions.

The string of a Path can be accessed as str(p).

To write to a file using pathlib:

textfile = Path(myfilepath)

# create file if it doesn't exist
textfile.touch()

# open file, write string, then close file
textfile.write_text(mystringtext)

# open file with context manager
with textfile.open('r') as f:
    f.read()
M.T
  • 4,917
  • 4
  • 33
  • 52
  • Thank you. It seems online tutoriaks are not enough. Woud you please recommend any good py3 book to read? – Ivan Baranov Aug 29 '17 at 12:42
  • @Ivan Baranov I think tutorials can be helpful in getting the basics down. At that point you will be much more capable of phrasing your questions/querys so that you can search for the answers you need. – M.T Aug 29 '17 at 14:33