I am new to programming and need input on how I could've written something better. Not coding on daily basis. The task was to write a script in python, bash or anything. So I mixed python with bash commands using python module os.systems
. Some friends recommended lists but I wanted the output to be similar as the example in this task:
Write a script (Bash, Python etc.) that checks for files - in directory X - that have not been modified in (older than) the last Y days.
The directory and days should be passed to the script as mandatory arguments.The script shall only look for files in directory X, not in sub directories. You may assume that none of the filenames contain newlines.
The output of the script should print the file names, and the time for when the file was last modified, sorted by modification time:
./README.txt 2019-05-09 17:19:53.193771720 +0200 ./README.txt.gpg 2019-05-09 17:20:21.331833720 +0200 ./migratemost-master.zip 2019-05-20 12:52:34.867119547 +0200 ./INC177759 2019-05-23 13:29:47.014557386 +0200
Include a help option, so that if '-h' or '--help' is passed as an optional argument, a summary of what the program does is printed to stdout.
Also try to handle user errors so the script exits gracefully with an error message upon incorrect - or missing - input.
My solution that I need help to refactor/improve peer review on:
import os
import sys
days = raw_input("Please enter days: ")
if days.isdigit():
print "You entered:", days
else:
sys.exit("Exiting the program, wrong data type.")
dir = raw_input("Please enter path: ")
print "You entered:", dir
stuff_in_string = "find {} -type f -mtime -{}".format(dir, days)
print stuff_in_string
print(os.system('{} | xargs -d \'\n\' ls -lth --full-time'.format(stuff_in_string)))
The output:
$ python main.py
Please enter days: asdf
Exiting the program, wrong data type.
$
$ python main.py
Please enter days: 365
You entered: 365
Please enter path: .
You entered: .
find . -type f -mtime -365
-rw-r--r--. 1 root root 413 2020-09-21 16:43:48.608029286 +0200 ./main.py
-rw-r--r--. 1 root root 0 2020-09-21 09:36:17.072137720 +0200 ./file2.csv
-rw-r--r--. 1 root root 0 2020-09-21 09:35:28.502502950 +0200 ./file1.txt
0
$
$ python main.py
Please enter days: 9999
You entered: 9999
Please enter path: .
You entered: .
find . -type f -mtime -9999
-rw-r--r--. 1 root root 413 2020-09-21 16:43:48.608029286 +0200 ./main.py
-rw-r--r--. 1 root root 0 2020-09-21 09:36:17.072137720 +0200 ./file2.csv
-rw-r--r--. 1 root root 0 2020-09-21 09:35:28.502502950 +0200 ./file1.txt
-rw-r--r--. 1 root root 0 2012-01-01 00:00:00.000000000 +0100 ./goldenfile.xls
0
main.py
from my perspective \$\endgroup\$main.py
is arbitrary. Not been modified in last 1 year? \$\endgroup\$I wanted the output to [look similar to] the example
have another look: the example lists file modified more than 365 days ago, in order of decreasing "age of modification". Your output shows "younger" files increasing in age. \$\endgroup\$2020-09-21 16:43:48.608029286 +0200
is the time file was last modified. I do not think this is outside of 1 year limit. \$\endgroup\$find
norls
to bebash
built-ins.) \$\endgroup\$