Date

Here's a lesson in how to reinvent the wheel and be scared of Bash. I.e. I'm going to recreate (most of) the ls -hla command.


First we can write a function to collect all file names in a specified path, and make it default to the current working directory. I've added an argument full that will return the full file path if set to True, which will come in useful when you need to specify an absolute file path:

import os
def lf(path='.', full=False):
    file_list = os.listdir(path)
    file_list = [os.path.join(path, f) for f in file_list] if full else file_list
    return file_list

For demonstrating the output, I'll examine the contents of a package I'm working on, rankings

>>> lf()
['.git',
 '.gitignore',
 'LICENSE',
 'rankings',
 'rankings.sublime-project',
 'rankings.sublime-workspace',
 'README.rst',
 'setup.cfg',
 'setup.py']

Second, a function to obtain the file sizes:

def lf_size(path='.'):
    file_paths = lf(path, full=True)
    return [os.path.getsize(fp) for fp in file_paths]
>>> lf_size()
[0L, 66L, 1343L, 0L, 45L, 23431L, 1085L, 41L, 809L]

Then one to indicate if it's a file or directory:

def lf_type(path='.'):
    dir_content = lf(path, full=True)
    dir_list = [os.path.isdir(dc) for dc in dir_content]  
    types = ['dir' if d else 'file' for d in dir_list]
    return types
>>> lf_type()
['dir', 'file', 'file', 'dir', 'file', 'file', 'file', 'file', 'file']

And the last modified date:

def lf_date(path='.'):
    epoch_time = [os.stat(f).st_mtime for f in lf(path, full=True)]
    fmt = '%Y-%m-%d %H:%M:%S'
    time_stamp = [time.strftime(fmt, time.gmtime(et)) for et in epoch_time]
    return time_stamp
>>> lf_date()
['2017-01-22 22:29:36',
 '2017-01-22 21:08:08',
 '2017-01-22 20:57:58',
 '2017-01-22 21:56:49',
 '2017-01-22 18:02:18',
 '2017-01-22 22:34:33',
 '2017-01-22 22:04:19',
 '2017-01-22 20:49:26',
 '2017-01-22 21:10:36']

Finally, put it all together:

def lf_all(path='.'):
    dir_details = zip(lf(path), lf_type(path), lf_size(path), lf_date(path))
    return dir_details
>>> lf_all()
[('.git', 'dir', 0L, '2017-01-22 22:29:36'),
 ('.gitignore', 'file', 66L, '2017-01-22 21:08:08'),
 ('LICENSE', 'file', 1343L, '2017-01-22 20:57:58'),
 ('rankings', 'dir', 0L, '2017-01-22 21:56:49'),
 ('rankings.sublime-project', 'file', 45L, '2017-01-22 18:02:18'),
 ('rankings.sublime-workspace', 'file', 23431L, '2017-01-22 22:34:33'),
 ('README.rst', 'file', 1085L, '2017-01-22 22:04:19'),
 ('setup.cfg', 'file', 41L, '2017-01-22 20:49:26'),
 ('setup.py', 'file', 809L, '2017-01-22 21:10:36')]

For better readability we can add an argument to put it in a dataframe with column names:

from pandas import DataFrame
def lf_all(path='.', as_df = True):
    dir_details = zip(lf(path), lf_type(path), lf_size(path), lf_date(path))
    if as_df:
        cols = ['name', 'type', 'bytes', 'modified']
        dir_details = DataFrame(dir_details, columns=cols)
    return dir_details
>>> lf_all()
                         name  type  bytes             modified
0                        .git   dir      0  2017-01-22 22:29:36
1                  .gitignore  file     66  2017-01-22 21:08:08
2                     LICENSE  file   1343  2017-01-22 20:57:58
3                    rankings   dir      0  2017-01-22 21:56:49
4    rankings.sublime-project  file     45  2017-01-22 18:02:18
5  rankings.sublime-workspace  file  23431  2017-01-22 22:34:33
6                  README.rst  file   1085  2017-01-22 22:04:19
7                   setup.cfg  file     41  2017-01-22 20:49:26
8                    setup.py  file    809  2017-01-22 21:10:36

All of this code is available in a single file named pyfiles.py that can be downloaded from my github here. Once you have this script, you can import all the functions by placing it into your current working directory, and then running from pyfiles import *.