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
  What does .flush do? How can I change this to write to the file? Pedroski55 3 164 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 424 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,528 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,452 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,604 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,106 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,594 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,979 Jan-21-2023, 06:57 AM
Last Post: jacklee26
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,124 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  how far is this line indented? Skaperen 3 1,330 May-30-2022, 05:49 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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