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 9140927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:34:49+00:00 2026-06-17T09:34:49+00:00

I am new to python and this may be a foolish question but it

  • 0

I am new to python and this may be a foolish question but it troubles me for several days.
I have about 30 log files, and each of them contains strings and data. They are almost the same except the difference of several data, and their names are arranged regularly, like ‘log10.lammps’, ‘log20.lammps’, etc.(The ’10”20’represent the temperature of the simulation). I want to write a python script, which loops all these files and read their data in a specific line, (say line3900). Then I want to write these data to another data file, which is arranged like these:

10 XXX
20 XXX
30 XXX
.
.
.

I can read and write from a single file, but I cannot achieve the loop. Could anyone please tell me how to to that. Thanks a lot!

PS. Still another difficulty is that the data in line 3900 is presented like this: “The C11 is 180.1265465616”, the one I want to extract is 180.1265465616. How can I extract the number without the strings?

  • 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-06-17T09:34:50+00:00Added an answer on June 17, 2026 at 9:34 am

    This answer covers how to get all the files in a folder in Python. To summarize the top answer, to get all the files in a single folder, you would do this:

    import os
    import os.path
    
    def get_files(folder_path):
        return [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
    

    The next step would be to extract the number from the line The C11 is 180.1265465616.
    I’m assuming that you have a function called get_line that given a filename, will return that exact line.

    You can do one of three things. If the length of the number at the end is constant, then you can just grab the last n characters in the string and convert it to a number. Alternatively, you could split the string by the spaces and grab the last item — the number. Finally, you could use Regex.

    I’m just going to go with the second option since it looks the most straightforward for now.

    def get_numbers():
        numbers = []
        for file in get_files('folder'):
            line = get_line(file)
            components = line.split(' ')
            number = float(components[-1])
            numbers.append(number)
        return numbers
    

    I wasn’t sure how you wanted to write the numbers to the file, but hopefully these should help you get started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Python so I may be going about this completely wrong, but
this may not be an earth-shattering deficiency of python, but i still wonder about
This may seem like the worlds simplest python question... But I'm going to give
I am pretty new to rails so this may be an easy question, but
I'm new to Python and CGI, so this may be trivial. But I'd like
I'm new in the Python world, coming from PHP. So, this question may sound
Disclaimer: This may be a terribly simple question but I'm attempting to learn Python.
This may be more of a question about how Python itself imports methods, which
this may sound like a strange question from a Python noob, but here's the
I am new to python so this may sound very basic. I have imported

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.