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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:56:48+00:00 2026-05-13T20:56:48+00:00

This seems to be an embarrassingly simple question, but, after a day of reading

  • 0

This seems to be an embarrassingly simple question, but, after a day of reading over How-To’s and manuals, it must be asked.

I’m writing many lines to a few files using a few nested loops, inserting some static strings and copying lines over from other files over and over again. The output appears to be a single copy of the static strings and all of the lines I want to copy from other files, instead of multiple copies of the strings combined with the copied lines.

I made a test script that I thought would mimic the behaviour, but it behaves perfectly:

for i in range(10):
    f = open('output.txt','w')

    f.write( "---------------------------\n" )

    FILE1 = open('test1.txt','r')
    for line in FILE1:
        f.write( "... compliments of loop #1 ...\n" )
        f.write( line )
    FILE1.close()

    f.write( "\n##########################\n" )

    FILE2 = open('test2.txt','r')
    for line in FILE2:
        f.write( "... compliments of loop #1 ...\n" )
        f.write( line )
    FILE2.close()

    f.write( "\n++++++++++++++++++++++++++\n" )

The output is as expected: the static strings interleaved with copied strings. My real, ugly script, however, doesn’t do this. I don’t want to paste the whole thing in here, but will include as much as I think is relevant (and I’ll probably get that wrong, too, ’cause I don’t know what’s going on). It references an array of objects – I won’t include the class, as it seems to behave.

for i in range(10):
    print "script-%s-%i-%s%s" % (cities[i].user,i,cities[i].name,cities[i].coords)

    f = open("script-%s-%i-%s%s" % (cities[i].user,i,cities[i].name,cities[i].coords),'w')

    f.write( "//\n" )
    f.write( "//\n// %s - %s            %s\n" % (cities[i].user,cities[i].name,cities[i].coords) )
    f.write( "//\n" )
    f.write( "//\n" )

    npc10 = open("script-%s-npc10-%i.txt" % (cities[i].user,i),'r')
    for line in npc10:
        f.write( "ifgosub ( m_city.AnyIdleHero(%s) == false ) wait_for_big_hero\n" % (cities[i].hero) )
        f.write( "ifgosub ( m_city.IsArmyReady(a:%i,s:%i,w:%i,wo:%i) == false ) gosub check_npc10\n" % (lvl10.arch,lvl10.scout,lvl10.warr,lvl10.work) )
        f.write( "ifgosub ( m_city.IsArmyReady(a:%i,s:%i,w:%i,wo:%i) == false ) farm_npc5\n" % (lvl10.arch,lvl10.scout,lvl10.warr,lvl10.work) )
        f.write( "ifgosub ( m_city.AnyIdleHero(%s) == false ) wait_for_big_hero\n" % (cities[i].hero) )
        f.write( line )
    npc10.close()

    f.write( "\n//\n" )
    f.write( "label farm_npc5\n" )

    npc5 = open("script-%s-npc5-%i.txt" % (cities[i].user,i),'r')
    for line in npc5:
        f.write( "sleep 5\n" )
        f.write( line )
    npc5.close()

    f.write( "\n//\n" )
    # ... 107 lines of static f.write's

    f.close()

Sample of one of the input files ( script-%s-npc10-%i.txt" % (cities[i].user,i) ) – they are all very similar:

attack 456,357 Alfred a:9215,t:185,wo:200,w:2000,s:200 //Distance: 1 Mission time: 8m 52s

attack 159,357 Alfred a:9215,t:185,wo:200,w:2000,s:200 //Distance: 1 Mission time: 8m 52s

attack 159,215 Alfred a:9215,t:185,wo:200,w:2000,s:200 //Distance: 1 Mission time: 12m 34s

Sample of one of the output files

( "script-%s-%i-%s%s" % (cities[i].user,i,cities[i].name,cities[i].coords) ):

//

// user1 – cityname1 (456,456)

//

//

ifgosub ( m_city.AnyIdleHero(Alfonso) == false ) wait_for_big_hero

ifgosub ( m_city.IsArmyReady(a:92150,s:2000,w:2000,wo:2000) == false ) gosub check_npc10

ifgosub ( m_city.IsArmyReady(a:92150,s:2000,w:2000,wo:2000) == false ) farm_npc5

ifgosub ( m_city.AnyIdleHero(Alfonso) == false ) wait_for_big_hero

attack 456,357 Alfred a:9215,t:185,wo:200,w:2000,s:200 //Distance: 1 Mission time: 8m 52s

attack 159,357 Alfred a:9215,t:185,wo:200,w:2000,s:200 //Distance: 1 Mission time: 8m 52s

attack 159,215 Alfred a:9215,t:185,wo:200,w:2000,s:200 //Distance: 1 Mission time: 12m 34s

//

label farm_npc5

sleep 5

attack 354,159 Alfred b:50,t:40 //Distance: 1 Mission time: 13m 20s

attack 789,654 Alfred b:50,t:40 //Distance: 2 Mission time: 26m 40s

attack 125,456 Alfred b:50,t:40 //Distance: 2 Mission time: 29m 48s

//

[…]

What’s the difference? Why don’t the static strings get repeated, while the copied lines do?

ANSWER: I wrote the source files in OSX TextEdit, so their newline character was '\r', as pointed out by gnibbler. Following his lead led me to 6 PEP 278: Universal Newline Support. Using the file mode 'rU' squared everything.

Thanks folks!

  • 1 1 Answer
  • 1 View
  • 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-13T20:56:49+00:00Added an answer on May 13, 2026 at 8:56 pm

    It appears that the for loop is only executing once.

    I’m not sure why it would do that – perhaps there is a problem with the line endings so the whole file is being read in as a single line.

    For example, if the lines of the input script file has \r line ending and python is expecting \n line endings.

    Try using ‘rU’ as the mode instead of ‘r’

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

Sidebar

Related Questions

this seems to be a simple question, but i cannot get my head around
This seems like a really simple question but I haven't seen it answered anywhere.
This seems like a simple to answer question, but I cannot figure out how
This seems to be a simple question but nevertheless I haven't found an answer
This seems like a simple question, but I can't find it with the Stack
This seems a 'stupid' question, but let me explain. I'm working for a company
This seems like a repeated question but i'm not able to get my answer.
This seems to be primary Question. but i search it and i couldn't find
This seems like such a simple issue but I cannot find an elegant solution.
This seems like the simplest Git question, but I can't find ANYTHING on it.

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.