Python Forum
How to print directory files (& timestamps)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print directory files (& timestamps)
#1
Question 
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
Reply
#2
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.
Reply
#3
(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
Reply
#4
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Opening files in same directory as script Emekadavid 3 2,551 May-28-2020, 06:50 PM
Last Post: Emekadavid
  traverses directory recursively and displays files Prani05 7 3,335 Apr-30-2020, 10:25 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020