Python Forum
How to write in text file - indented block
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write in text file - indented block
#1
Hi,

I would like to open a text file in the loop and every time I would like to save a new line in the file but I have syntax error of "expected an indented block". How to fix that ?

i = 1
while i < 6:
f = open("readme.txt", "a")
f.write( str(i) + "\tHello\t" + "Now the file has more content!\n")
f.close()
i += 1
Yoriz write Aug-09-2021, 06:34 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
To fix it.
f = open("readme.txt", "a")
i = 0
while i < 6:
    f = open("readme.txt", "a")
    f.write( str(i) + "\tHello\t" + "Now the file has more content!\n")
    i += 1
    f.close()
I would not write it like this,code ove can be called unpytonic.
with open("readme.txt", "a") as f:
    for i in range(6):
        f.write(f'{i} \tHello\t Now the file has more content!\n')
So with open close file object automatically,for loop is better more readable than the while.
Reply
#3
(Aug-09-2021, 12:08 PM)snippsat Wrote: To fix it.
f = open("readme.txt", "a")
i = 0
while i < 6:
    f = open("readme.txt", "a")
    f.write( str(i) + "\tHello\t" + "Now the file has more content!\n")
    i += 1
    f.close()
I would not write it like this, code ove can be called unpytonic.
with open("readme.txt", "a") as f:
    for i in range(6):
        f.write(f'{i} \tHello\t Now the file has more content!\n')
So with open close file object automatically,for loop is better more readable than the while.

I still get an error when I run the following code. I would like to write the serial data into the file.

import serial
ser = serial.Serial("COM7", 9600)
    while True:
    bs = ser.read(512)
    bs = bs.replace(b'\n', b' ').replace(b'\r', b' ')
    print(bs)
    f = open("readme.txt", "a")
    f.write( bs)
    f.close()
buran write Aug-09-2021, 12:58 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info. Using quote tags does not help to see the problem with indentation
Reply
#4
Please use python tags. I cannot tell if you error is that you don't indent, or if that is just an artifact of using "Block Quote" instead of "Insert python".

Please post your entire error message and trace, and post the code that crashed. The entire code. It is really annoying when the useful line numbers in the error trace do not match up with the provided code.
ndc85430 likes this post
Reply
#5
Even though it's been a while, I'm wondering where the bug was and if the fix provided here helped? I just wanted to use similar code for my essay search project on the topic and save them to a file through writing. I've already looked into having them from https://studymoose.com/free-essays/stakeholders on economics for example and even figured out how to copy them. But that's just if there are several of them it is more convenient through a loop, but the same error appears and thought there was already a solution.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,118 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
  how to write/overwrite data in a txt. file according to inp Quinn 2 1,587 Aug-12-2025, 04:20 PM
Last Post: Quinn
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 36,670 May-20-2025, 12:26 PM
Last Post: hanmen9527
  How to write variable in a python file then import it in another python file? tatahuft 4 2,253 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,193 Oct-17-2024, 01:15 AM
Last Post: Winfried
  What does .flush do? How can I change this to write to the file? Pedroski55 3 2,259 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 2,852 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 8,640 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 5,857 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 35,977 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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