Saturday, May 22, 2021

Find and display files greater than 256 bytes

Script

#!/usr/local/bin/python3.5

import os

target = input("Enter the name of the directory: " )
print()

for current_dir, sub_dirs, files in os.walk(target):
        for filename in files:
                complete_path = os.path.join(current_dir, filename)
                try:
                        filesize = os.path.getsize(complete_path)
                except OSError as e:
                        continue
                if filesize > 256:
                        print("This file is greater than 256 bytes: ", complete_path)
print()


Background

ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr
total 24
-rw-r--r--. 1 root root    0 Feb 26 23:02 PortofSpain-1976-K02.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Chennai-2008-K15.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Oval-1971-K01.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Madras-1998-K08.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Lords-2014-K17.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Delhi-1999-K09.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Mumbai-2016-K19.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Kanpur-1996-K07.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Mohali-2010-K16.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Lords-1986-K04.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Kolkata-2005-K13.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Melbourne-1977-K03.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Calcutta-1993-K05.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Perth-2008-K14.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Vishakapatnam-2016-K18.txt
-rw-r--r--. 1 root root    0 Feb 26 23:02 Ahmedabad-1996-K06.txt
-rw-r--r--. 1 root root  629 Feb 27 19:44 Kolkata-2001-K10.txt
-rw-r--r--. 1 root root    0 Feb 27 19:45 Zaheer-Khan-Man-of-the-Match.txt
drwxr-xr-x. 5 root root   66 Feb 27 20:09 Victories-over-Australia
-rw-r--r--. 1 root root 2403 Feb 28 16:48 Adelaide-2003-K12.txt
-rw-r--r--. 1 root root    0 Feb 28 22:48 Old-Trafford-1990-K25.txt
-rw-r--r--. 1 root root  328 Feb 28 22:48 Headingley-2002-K11.txt
drwxr-xr-x. 3 root root 4096 Feb 28 22:48 .
-rw-r--r--. 1 root root  328 Feb 28 22:48 Dravid-master-class-148.txt
drwxr-xr-x. 3 root root 4096 Mar  2 14:27 ..
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >

Execution

Enter the name of the directory: /Python-scripts/Memorable-Tests

This file is greater than 256 bytes:  /Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt
This file is greater than 256 bytes:  /Python-scripts/Memorable-Tests/Kolkata-2001-K10.txt
This file is greater than 256 bytes:  /Python-scripts/Memorable-Tests/Headingley-2002-K11.txt
This file is greater than 256 bytes:  /Python-scripts/Memorable-Tests/Dravid-master-class-148.txt








No comments:

Post a Comment