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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:37:55+00:00 2026-06-05T02:37:55+00:00

Ok, so basic python question. I have a simple script to replace text on

  • 0

Ok, so basic python question. I have a simple script to replace text on html(txt) files. I wrote some code and put in some newline html that had xhtml coding i wanted to replace so I tried to write a python script to replace the xhtml coding versions with regular newline html.
Sometimes when I run the code, it works fine, but sometimes it rewrites the file, and at the end the last two characters are repeated after a newline. I’m running 2.7 on OS X Lion, if that matters. Here’s the code:

import sys
import re

def replace_text(filename): 
    with open(filename, 'r+') as f:
        p = re.compile( '(</br>|<br/>|<br />)')
    f_data = open(filename, 'r+').read()
    f.write(p.sub('<br>', f_data))
    f.close()

def main():
args = sys.argv[1:]

if not args:
    print 'usage: [--summaryfile] file [file ...]'
    sys.exit(1)

summary = False

if args[0] == '--summaryfile':
    summary = Trye
    del args[0]

for filename in args:
    replace_text(filename)

if __name__ == '__main__':
main()

So when I run it with a file (say ‘foo.txt’) that has the content:

</br> <br/> <br /> <br> poop

sometimes it outputs:

<br> <br> <br> <br> poop

and sometimes it outputs:

<br> <br> <br> <br> poop
op

huh? Using OS X Lion with python 2.7. I’m missing something simple? Also using Gedit 3.2.6 as an editor. Any help?

  • 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-05T02:37:56+00:00Added an answer on June 5, 2026 at 2:37 am

    You’re shortening the content of the file, but you’re not shortening its length. Don’t forget to do so via .truncate() before closing.

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

Sidebar

Related Questions

I've been doing some simple web programming using python, and I have a basic
I have a fairly basic question regarding python lists/dictionaries which I would like some
I have written some physics simulation code in C++ and parsing the input text
I have some trouble trying to split large files (say, around 10GB). The basic
first post, so play nice! I have a fairly basic question about Python dictionaries.
I have a basic question on python functions and parameters Given this function: def
I'm new to Python and have what is probably a very basic question about
Very basic question - how to get one value from a generator in Python?
[Sorry, I'm new in Python. Although it seems to be a very basic question,
I'm starting a basic application using Python and PyQt and could use some experienced

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.