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

  • Home
  • SEARCH
  • 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 7163751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:57:19+00:00 2026-05-28T13:57:19+00:00

I have a code that search if a line begin with a specified word

  • 0

I have a code that search if a line begin with a specified word and if it does, it changes that whole line with a specified input. However, it doesn’t work for some lines if the line is indented by spaces? Is there a way to read the text directly and ignore the spaces.

Here is the code: (with comments on where the problem is)

import os

def template(filein):
    currdir = os.getcwd() # get current directory
    new_file = open(os.path.join(currdir,'maindir','template.in'),'wt')
    old_file = open(filein)
    for line in old_file:
        if line.startswith('   indent'):
            # this part works well because I put the exact number of spaces present in the text before the search word
            new_file.write('   indent == %s \n' % str('%(indent)s'))
        elif line.startswith('noindent'):
            # this part can't find noindent because i didn't specify the spaces before that that is present in the text
            new_file.write('noindent == %s \n' % str('%(noindent)s'))
        else:
            new_file.write(line)
    new_file.close()
    old_file.close()

Thanks

EDIT: I want to preserve all the spaces present in the original file, even in the lines that I modified.

  • 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-28T13:57:19+00:00Added an answer on May 28, 2026 at 1:57 pm

    You can use lstrip to remove the whitespace from the beginning (left) of a line:

    for line in old_file:
        stripped_line = line.lstrip()
        # do your matching here against `stripped_line` instead of `line`
        # `line` still contains the original, non-stripped line
    

    On a sidenote, I recommend using with open('filename') as new_file, instead of what you’re doing now. This creates a block in which the file is available and makes sure the file is closed at the end of the block. See the end of this section in the docs.

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

Sidebar

Related Questions

I have some code that starts a background process for search in my WPF
I have several columns that I'd like to search against. My code might not
I have code that references a web service, and I'd like the address of
I have code that looks like the following, which works fine for displaying the
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that uses Win API function RegSaveKeyEx to save registry entries to
I have code that uses jquery.slideup and jquery.slidedown How can i know that div
I have code that calls an ENTRY in a SUBROUTINE before the SUBROUTINE .

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.