Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:02:13+00:00 2026-05-26T21:02:13+00:00

I wrote a script to find a certain string and print a certain occurrence.

  • 0

I wrote a script to find a certain string and print a certain occurrence. This worked very well until the groups become an indeterminate length and I simply need to print the last one. I was curious for the found2.group and found3.group if there was a way to simply only print the last result.

f = open ("CompTime.csv","w")
for infile in glob.glob( os.path.join(dir, '*.out') ):
    file_handler = open(infile, "r")
    content = file_handler.read()
    file_handler.close()
    #Find Real Time
    found2 = re.search(' REAL TIME  *.+', content)
    rtime = found2.group(0)[1:-1]
    #Find CPU Time
    found3 = re.search(' CPU TIMES  *.+', content)
    ctime = found3.group(0)[1:-1]
    #Create and Format the results.
    tResult = str(rtime)+','+str(ctime)
    f.seek(0,2) 
    f.write(tResult+'\n')
f.close()

Is there any way to do this simply I have read the literature on the regular expressions but I seem to be failing at completing this.

Working Version:

dir = os.getcwd()
for infile in glob.glob( os.path.join(dir, '*.out') ):
    file_handler = open(infile, "r")
    content = file_handler.read()
    file_handler.close()
    rtime = re.findall(' REAL TIME  *.+', content)[-1]
    #Find CPU Time
    ctime = re.findall(' CPU TIMES  *.+', content)[-1]
    #Create and Format the results.
    tResult = str(rtime)+','+str(ctime)
    print tResult
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:02:14+00:00Added an answer on May 26, 2026 at 9:02 pm

    I believe that re.match only looks for first occurence, you can use re.findall(pattern, string) instead:

    >>> re.findall('-[a-zA-Z]', 'ls -A -H -B -b .')
    ['-A', '-H', '-B', '-b']
    

    Then you can just access it like any other list:

    >>> re.findall('-[a-zA-Z]', 'ls -A -H -B -b .')[-1]
    '-b'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this script to find all files/directories to which $WWWUSER has write permissions.
I wrote a tiny Bash script to find all the Mercurial changesets (starting from
I wrote this script which counts occurrences of particular pattern in a given file.
Update: I wrote a working script that finishes this job in a reasonable length
I wrote this script some months ago, and now rereading it, I'm unable to
So I wrote this short script (correct word?) to download the comic images from
I just wrote this backup script: #!/usr/bin/bash # the number of days to do
I wrote this script to first install the msi and then copy my application
I wrote a little script that exports certain Excel cell values into Word. However,
I find it much easier to write MXML classes with embedded Script than trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.