Home > Back-end >  Find and show only files from a specific date given
Find and show only files from a specific date given

Time:04-15

Basically I have a tacacs file folder with a LOT of archived files, 3 are created per day to be precise. I'm looking to make a script where the user has to enter a date when he starts the script and it shows him the files created (or last modified) on that date.

I also plan to make it so that the user must then choose between the 3 files which one he wants to see. But for now I'm just trying to show the three files on the cli.

But my script doesn't accept the date I give it. I get each time "SyntaxError: invalid syntax" while showing me the month I entered.

      Traceback (most recent call last):
  File "scriptacacs2.py", line 6, in <module>
    date_entry = input('Enter a date in YEAR, MONTH, DAY format \n')
  File "<string>", line 1
    2022 04 12
          ^
SyntaxError: invalid syntax

The code is:

import re
import sys
import os
from datetime import datetime

date_entry = input('Enter a date in YEAR, MONTH, DAY format \n')
path = "/applis/tacacs/log/"
dirs = os.listdir(path)

mtime = datetime(date_entry).timestamp()

list_of_files = [
    file for file in glob.glob(path   '*.gz')
        if os.path.getmtime(file) == mtime
]
print (list_of_files)

I don't see what causes this syntax error. What's wrong with my code?

CodePudding user response:

I'm not sure why you're getting a SyntaxError error. I'm guessing it's because you're using Python2 and input behaves differently (it tries to parse the input) for python2 you should use raw_input.

But the parsing of the date is incorrect.

If you want it to parse a date in a 2022,01,01 format you should use:

datetime.strptime("2022,01,23", "%Y,%m,%d") or if you want to be able to have the whitespace in do:

datetime.strptime(re.sub("\s ", "", "2022, 01, 23"), "%Y,%m,%d")

Also you probably don't want to test for equality of the timestamp, you want to check for equality of date. So I'd do:


import re
import os
import glob
from datetime import datetime, timedelta

date_entry = input('Enter a date in YEAR, MONTH, DAY format \n')
date = datetime.strptime(re.sub("\s ", "", date_entry), "%Y,%m,%d").date()

path = "/applis/tacacs/log/"


list_of_files = [
    file for file in glob.glob(path   '*.gz')
    if date == datetime.fromtimestamp(os.path.getmtime(file)).date()
]
print(list_of_files)

CodePudding user response:

Your first problem is that you try to provide the datetime object a string, however, he needs integers. (see the documentation). So first, you need to split the date you receive with the split method. (here I changed a bit the input so as to have a more readable and practicable sentence). Then, you need to transform the numbers as string into integers with the map function and finally unpack the values with the * to pass the arguments.

Your second problem was the use of the glob library while you already retrieved the files with the listdir method.

And finally, you are checking the date of the files with the timestamp with is an error because the timestamp value represent the amount of secs since Jan 01 1970 and if a file is created one second after the other, they wont have the same timestamp. So, you have to convert these timestamps into dates and then check if the dates are the same.

Here's the final result :

import os

from datetime import datetime

PATH = "applis/tacacs/log/"


def is_same_date(t1, t2):
    """
    Check if the two provided timestamps represents the same date
    """
    return datetime.fromtimestamp(t1).date() == datetime.fromtimestamp(t2).date()


date_entry = map(int, input("Enter a date in 'YYYY/MM/DD' format \n").split("/"))

dirs = os.listdir(PATH)

mtime = datetime(*date_entry).timestamp()

list_of_files = [f for f in dirs if is_same_date(os.path.getmtime(PATH   f), mtime)]
print(list_of_files)

  • Related