Tuesday, May 7, 2013

Listing files and directories using Python

This is an alternative to Linux ls command. A python script to list files and directories on Linux.

#!/usr/bin/python
import os,sys
def getSize(name):
    st = os.stat(name)
    return st.st_size
try:
    dirPath=str(sys.argv[1])
except:
    dirPath=str(os.getcwd())

for i in os.listdir(dirPath):
    if os.path.isdir(i):
        print 'Directory ',getSize(i),os.path.getatime(i),i
    elif os.path.islink(i):
        print 'Link ',getSize(i),os.path.getatime(i),i
    else:
        print 'File ',getSize(i),os.path.getatime(i),i

1 comment:

  1. Learn Amazon Web Services for excellent job opportunities from Infycle Technologies, the Excellent AWS Training in Chennai. Infycle Technologies gives the most trustworthy AWS course in Chennai, with full hands-on practical training from professional trainers in the field. Along with that, the placement interviews will be arranged for the candidates, so that, they can meet the job interviews without missing them. To transform your career to the next level, call 7502633633 to Infycle Technologies and grab a free demo to know more

    ReplyDelete