Search code examples
pythonls

Python: How to store output of ls -lrt


Problem : In python, how to store the o/p of 'ls -lrt' in a string so that we can search for files of a particular pattern

This is inorder to find all files in a directory that has been modified between date1 and date2

Thanks in advance


Solution

  • >>> import subprocess
    >>> op = subprocess.check_output(["ls","-lrt"])
    >>> print(op)
    total 0
    drwxr-xr-x+   4 henry  staff   136 16 Jun 11:20 Public
    drwx------+   3 henry  staff   102 16 Jun 11:20 Movies
    drwx------+   4 henry  staff   136 16 Jun 12:54 Pictures
    drwx------@  48 henry  staff  1632 25 Jun 21:35 Library
    drwx------+  39 henry  staff  1326 28 Jun 09:15 Desktop
    drwx------+   8 henry  staff   272 28 Jun 12:24 Documents
    drwx------+  19 henry  staff   646 29 Jun 15:15 Music
    drwx------+ 146 henry  staff  4964 30 Jun 17:49 Downloads