Python Forum
[3.4.4] Last loop element partially done
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[3.4.4] Last loop element partially done
#1
Hi,

I'm pretty new to Python so my IDE is based on a tuto (because the final goal will use Qt4). So, for the moment, I used the strict version as in the tuto.

Here's my code :
url = 'https://www.test.com'

for i in range(5):
  print(url + str(i))
  print('dl')
  f = open(str(i) + ".html", "wt")
  f.write('test')
  f.close

print ('end')
All files are generated but the last one is always empty. Did I miss something ?

Thanks,
Vincent

(seems to be the same problem with 3.6.5 both under Windows environment)
Reply
#2
I see one mistake: f.close should be f.close(). Can you describe precisely all the files' content after you run the code?
Reply
#3
it's better to use with:
for i in range(5):
    with open('{}.html'.format(i), "w") as f:
        f.write('test')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(May-08-2018, 10:50 AM)Gribouillis Wrote: I see one mistake: f.close should be f.close().

YES !!! :) Thanks, it works.

(May-08-2018, 10:59 AM)buran Wrote: it's better to use with:
for i in range(5): with open('{}.html'.format(i), "w") as f: f.write('test')

very more compact version. Harder for me to read for the moment but I keep in mind this way. thanks.
Reply
#5
with is context manager. It will close the file for you, so no need to use f.close()
.format() is better than string concatenation - e.g. no need to cast the int to str
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(May-08-2018, 10:01 AM)nolme Wrote: (seems to be the same problem with 3.6.5 both under Windows environment)
With 3.6 can also make it more readable with f-string.
for numb in range(5):
    with open(f'{numb}.html', "w") as f:
        f.write('test')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,844 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  for loop not plotting all element of 'i' amjass12 0 1,169 Aug-18-2020, 09:53 AM
Last Post: amjass12
  iterating an interator partially then resuming Skaperen 3 2,143 Apr-30-2019, 03:49 AM
Last Post: Skaperen
  Unable to locate element no such element gahhon 6 4,466 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Accessing first list element when at end of loop! JY450 1 2,231 Aug-14-2018, 12:50 PM
Last Post: perfringo
  Change single element in 2D list changes every 1D element AceScottie 9 12,066 Nov-13-2017, 07:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020