Python Forum
How to print directory files (& timestamps) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to print directory files (& timestamps) (/thread-39067.html)



How to print directory files (& timestamps) - chatguy - Dec-29-2022

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


RE: How to print directory files (& timestamps) - ndc85430 - Dec-29-2022

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.


RE: How to print directory files (& timestamps) - chatguy - Dec-29-2022

(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


RE: How to print directory files (& timestamps) - ndc85430 - Dec-29-2022

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?