Home > Software design >  How do I add a CLI argument to find files >, <, or = a given size?
How do I add a CLI argument to find files >, <, or = a given size?

Time:05-04

I am basically trying to have three arguments for CLI user input:

  1. Folder path
  2. <, >, or =
  3. File size

With these arguments, I want my application to fetch information for all files in the folder path that meet the size arguments. Then I want it to print the file name, size, and date created. Here is what I have so far:

import sys, os, time
try:
    folder = sys.argv[1]
    fileSize = int(sys.argv[2])
    
except IndexError:
    print("Input folder path, > < =, and file size in KB's.")
    sys.exit()
except ValueError:
    print("File size in KB's after path.")
    sys.exit()

# Get all files
for (root, dir, files,) in os.walk(folder):
    for file in files:
        path = os.path.join(root, file)
        size = os.path.getsize(path)/1024
        seconds = os.path.getctime(path)

        if size > fileSize:
            print("Name:", file)
            print("Size: %.2f KB"% size)
            print("Created: ",time.strftime("%d %B %Y",time.localtime(seconds)))
            print()

How do I add an argument to allow the user to define what size files the application will fetch by using greater than, less than, or equal to symbols? Currently it is finding files that are greater than. Thank you!

CodePudding user response:

Currently you are taking arguments only for folder and for fileSize, based on IndexError message I assume you would want to have something like this in args:

C:\Users\xxx\ > 2

But based on ValueError message you want size second, doesn't matter the order in this case you should also take one more argument that will check whatever sign is in input, it will already be a string and you can do checking on fileSizes depending on that sign later on just check what is the sign

  • Related