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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:18:58+00:00 2026-05-30T06:18:58+00:00

The lazy me is thinking about adding a column to some textfiles. The textfiles

  • 0

The lazy me is thinking about adding a column to some textfiles.

The textfiles are in directories and I would like to add the directory name to the text file.

Like the text file text.txt in the folder the_peasant:

has a wart    
was dressed up like a witch     
has a false nose

would become:

the_peasant has a wart    
the_peasant was dressed up like a witch    
the_peasant has a false nose

Then I have similar text files in other folders called “the_king” etc.

I would think this is a combination of the find command, bash scripting and sed but I cant see it through. Any ideas?

  • 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-30T06:19:00+00:00Added an answer on May 30, 2026 at 6:19 am

    The directory tree:

    % tree .
    .
    ├── the_king
    │   └── text.txt
    ├── the_knight
    │   └── text.txt
    ├── the_peasant
    │   └── text.txt
    └── wart.py
    3 directories, 4 files
    

    Directories and contents before:

    % find . -name 'text.txt' -print -exec cat {} \;       
    ./the_king/text.txt
    has a wart    
    was dressed up like a witch     
    has a false nose
    ./the_knight/text.txt
    has a wart    
    was dressed up like a witch     
    has a false nose
    ./the_peasant/text.txt
    has a wart    
    was dressed up like a witch     
    has a false nose
    

    Code (wart.py):

    #!/usr/bin/env python
    
    import os
    
    text_file = 'text.txt'
    cwd = os.path.curdir # '.'
    
    # Walk thru each directory starting at '.' and if the directory contains
    # 'text.txt', print each line of the file prefixed by the name containing
    # directory.
    for root, dirs, files in os.walk(cwd):
        if text_file in files: # We only care IF the file is in this directory.
            print 'Found %s!' % root
            filepath = os.path.join(root, text_file) # './the_peasant/text.txt'
            root_base = os.path.basename(root)       # './the_peasant' => 'the_peasant'
            output = ''
            with open(filepath, 'r') as reader:      # Open file for read/write
                for line in reader:                  # Iterate the lines of the file
                    new_line = "%s %s" % (root_base, line)
                    print new_line,
                    output += new_line               # Append to the output
    
            with open(filepath, 'w') as writer:
                writer.write(output)                 # Write to the file
    
            print
    

    Which outputs:

    Found ./the_king!
    the_king has a wart    
    the_king was dressed up like a witch     
    the_king has a false nose
    
    Found ./the_knight!
    the_knight has a wart    
    the_knight was dressed up like a witch     
    the_knight has a false nose
    
    Found ./the_peasant!
    the_peasant has a wart    
    the_peasant was dressed up like a witch     
    the_peasant has a false nose
    

    Directories and contents after:

    % find . -name 'text.txt' -print -exec cat {} \;
    ./the_king/text.txt
    the_king has a wart    
    the_king was dressed up like a witch     
    the_king has a false nose
    ./the_knight/text.txt
    the_knight has a wart    
    the_knight was dressed up like a witch     
    the_knight has a false nose
    ./the_peasant/text.txt
    the_peasant has a wart    
    the_peasant was dressed up like a witch     
    the_peasant has a false nose
    

    This was fun! Thanks for the challenge!

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

Sidebar

Related Questions

I'm adding some lazy initialization logic to a const method, which makes the method
I've been thinking about writing a text editor control that can edit text that
I was thinking about the classic issue of lazy singleton initialization - the whole
Lazy programmer alert. :) Cassandra stores column values as bytes ( Java example ).
Is Subversion's 'Lazy Copy' still lazy when overwriting a previously deleted file? I store
Sure I'm missing out something or not thinking about a better way to accomplish
I'm thinking about using the Open Session In View (OSIV) filter or interceptor that
I've been thinking of some concepts underlying a new language. It was kind of
while learning a bit about hibernate i came across famous lazy initialization exception under
I have an incoming lazy stream lines from a file I'm reading with tail-seq

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.