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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:19:15+00:00 2026-05-15T21:19:15+00:00

I have a directory with 260+ text files containing scoring information. I want to

  • 0

I have a directory with 260+ text files containing scoring information. I want to create a summary text file of all of these files containing filename and the first two lines of each file. My idea was to create two lists separately and ‘zip’ them. However, I can get the list of the filenames but I can’t get the first two lines of the file into an a appended list. Here is my code so far:

# creating a list of filename
for f in os.listdir("../scores"):
    (pdb, extension) = os.path.splitext(f)
    name.append(pdb[1:5])

# creating a list of the first two lines of each file
for f in os.listdir("../scores"):
    for line in open(f):
        score.append(line)
        b = f.nextline()
        score.append(b)

I get an error the str had no attribute nextline. Please help, thanks in advance.

  • 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-15T21:19:15+00:00Added an answer on May 15, 2026 at 9:19 pm

    The problem you’re getting is a result of trying to take more than one line at a time from the scores file using a file iterator (for line in f). Here’s a quick fix (one of several ways to do it, I’m sure):

    # creating a list of the first two lines of each file
    for f in os.listdir("../scores"):
        with open(f) as fh:
            score.append(fh.readline())
            score.append(fh.readline())
    

    The with statement takes care of closing the file for you after you’re done, and it gives you a filehandle object (fh), which you can grab lines from manually.

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

Sidebar

Related Questions

I have directory with > 1000 .html files, and would like to check all
I have a directory with lots of files in them. I just want to
I have a directory of arbitrary files I want to include in my jar
I have a directory with a number of .py files in it. each file
I have a directory with a ton of files I want to find the
I am having many text files scattered across many subdirectories. Just want to have
I have a directory that stores all the .py files. bin/ main.py user.py #
I have PDB(text) files which are in a directory. I would like to print
I have a directory with files, I need to get a listing of these
I have a directory with PDF files that I need to create an index

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.