[Tutor] loop print issue
Alan Gauld
alan.gauld at btinternet.com
Fri Oct 11 11:32:23 EDT 2019
Please do not hijack someone else's thread to raise a new issue
it makes searching the archives much more difficult. Raise a new
message to start a new topic. (Note that simply changing the
subject line does not change the thread identifier in the message)
On 11/10/2019 16:00, ose micah wrote:
> I need your quick help, I am same loop print issue you pointed out.
> I have a file "xlist_file" with a list of varying values of up to 70.
> from this file I intend to split the content to two different files
> output1.txt and output2.txt. I want this values to be always fresh,
> making use of w+ and no appending. currently if I switch +a to +w in
> the output2.txt, it always cleans out the values.
>
>
> "line of code:"
>
> /sys.stdout = open('output1.txt','w+')/
> /
This is the main problem. You are using sys.stdout for file output.
That is not what sys.stdout is intended for and if you continue
to do so you will continue to have these kinds of issues.
You will also confuse yourself and anyone else who reads your code.
It is possible to do it but it requires you to do a whole heap of
extra work that would not be necessary if you just used regular
files.
OK, To the code...
You open sys.stdout on output1.txt here. Once only at the start of the
code. You never open it again.
> /
> /for i in xlist_file:/
> / if counter < 36:/
You are comparing counter even though you never initialize it? I'm
assuming you missed a line above that sets counter to 0?
> / if counter >= 35:/
> / sys.stdout.close()/
Here you close sys.stdout (output1.txt on the first occurence)
> / sys.stdout = open('output2.txt','a+')/
And now you open sys.stdout again with output2.txt.
On the first time round this closes output1 and opes output2.
On subsequent occurences it will close output2 then immediately reopwen
it again.
Notice that on each opening you will reposition the file cursor
to the beginning of the file if you use 'w+' and at the end of
the file if you use 'a+'. I'm not sure which you actually want.
And I'm even less sure why you think you want the plus version.
(That brings yet more complexity that you don't want or need)
> / print(i)/
> / counter +=1/
> / else:/
> / print(i)/
> / counter +=1/
You are using counters so you are writing the first 36 lines(0-35)
to output1. Then you write the rest to output2. If that is what
you want it would be a lot easier to say so explicitly:
with open('output1.txt','w') as output1
for count in range(36):
output1.write(xlist_file.readline())
with open('output2.txt','w') as output2:
for line in xlist_file:
output2 write(line)
No need for resetting sys.stdout.
No need for fancy '+' modes.
No need for complex counters or if statements.
However, I have a sneaky suspicion this is not actually what
you want to do. But either way it will almost certainly be
easier if you stop abusing sys.stdout and just open two
output files and write() to them!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list