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!