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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:08:42+00:00 2026-06-08T17:08:42+00:00

I am trying to accomplish what seems to be a simple task but I

  • 0

I am trying to accomplish what seems to be a simple task but I seem to be unable to figure it out. I am a Python rookie.

Here is my task:
I have a text file that contains a list of numbers, each on a separate line…

100101
100201
100301
100401
100501
100601
100701
100801
100901
101001

What I would like to do is read from the text file and write a new text file for each line in the file that contains the line and is also named using the line…

100101.txt
contains one line of text "100101"

100201.txt
contains one line of text "100201"

etc...

Hope this makes sense…thanks!

hjnathan

  • 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-08T17:08:43+00:00Added an answer on June 8, 2026 at 5:08 pm

    Try this:

    with open('data.txt') as inf:
        for line in inf:
            num = line.strip()
            if num:
                fn = '%s.txt' %num
                with open(fn, 'w') as outf:
                    outf.write('contains one line of text "%s"\n' %num)
    

    Using the with construct makes sure each file is closed when it’s not needed any longer (or if an exception should occur)

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

Sidebar

Related Questions

I'm trying to accomplish one simple task (but is not simple for me). I
I'm trying out Rubymotion and can't seem to do figure how to accomplish what
Im trying to accomplish something which may seem a bit convoluted but would be
I'm trying to accomplish a simple thing. I have a 2 levels menu with
I am trying to accomplish the following logical operation in Python but getting into
I'm trying to accomplish a simple task of creating a database with 2 tables
I am trying to accomplish something quite simple, yet I have found no good
There should be a simple Linq query for what I'm trying to accomplish, but
I am trying to accomplish a very simple task of loading a WebView that
Hello helpful stackoverflow members, I'm trying to accomplish what seems to be a simple

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.