Python Forum

Full Version: How to print directory files (& timestamps)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please pardon my simple question (Unfortunately Google searches haven't been much luck)

With Python, how do I print all files in a directory where:
1) Filenames start with FAIL*
2) Show file timestamps
3) Print in alphabetic Order
4) Extra credit: Show numbered lines (it would make it easier to read)

Would love to see output like the following please:

  1. Dec 26 22:17 FAIL_ReasonA_Test1.log
  2. Dec 25 14:21 FAIL_ReasonA_Test19.log
  3. Dec 26 11:06 FAIL_ReasonB_Test3.log
  4. Dec 26 22:21 FAIL_ReasonB_Test14.log
  5. Dec 11 12:21 FAIL_ReasonC_Test9.log
ps: Timestamps can say 12/26 or Dec 26, whichever is easier

Thanks in advance!
CG
We aren't here to write the code for you. I'm curious about what you did find in a search, though. What about the os module? For a start, it contains a function scandir that may be useful.
(Dec-29-2022, 04:20 AM)ndc85430 Wrote: [ -> ]We aren't here to write the code for you. I'm curious about what you did find in a search, though. What about the os module? For a start, it contains a function scandir that may be useful.

I ended up using the below.
It works, but I would have preferred a more native Python approach instead of using a BASH command.

command = "ls -l FAIL* |awk \'{print $6,$7,$8,$9}\' |nl"
os.system(command)
No worries. Thanks anyway though.

CG
You really don't need to use Bash at all. It's certainly possible to write it in pure Python. Have you even looked at os.scandir as I suggested?