Code Examples

Directory Traversal

import os
import sys

for (path, dirs, files) in os.walk(sys.argv[1]) :
    # print(path, dirs, files)
    depth_from_path = len(path.split('/'))
    print("-" * depth_from_path * 2, path.split('/')[-1])
    for file in files :
        print ('-' * depth_from_path * 4, file)

File State

import os
import pwd
import sys

fileStat = os.stat(sys.argv[1])
# print(fileStat)

if fileStat :
	# get file size in bytes 
    print('Filename: %s' %sys.argv[1])
    print('Mode: %s' %fileStat.st_mode)
    print('Size in bytes: %d' %fileStat.st_size) 
    print('Owner: uid is %d, username is %s' %( fileStat.st_uid, pwd.getpwuid(fileStat.st_uid).pw_name))

Last updated