Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help writing to files
#1
the code below the following error
IndexError: list index out of range
can someone help me please! 
import os.path

list_ = []
breaking_string = '  '
print("Start Typing: ")
while True:
    line = input()
    if line == '  ':
        break
    else:
        list_.append(line)

#tf_name = input("Enter text file name: ")
tf_name = 'list'

if os.path.isfile(tf_name) == True:
    file = open(tf_name + '.txt', "a")

else:
    file = open(tf_name + '.txt', "w")

counter = 0
while len(list_) >= counter:
    file.write(list_[counter])
    counter += 1
file.close()
Reply
#2
List index out of range means that you are using a index in [] which is too big. You are not givigin the full stack trace but there is nly one place in your code where you use that, so have you tried to add a print instruction to check the value/contents of list_ where you index it?

PS: IMHO list_ is a despicable variable name, because list would also be if it where not a reserved name in Python. List of what?

PPS: Looking  at your code, this:
counter = 0
while len(list_) >= counter:
    file.write(list_[counter])
    counter += 1
is much better written:
for l in list_:
    file.write(l)
and circumvents the explicit use of a counter (which, by the way, should go from 0 to len(list_)-1, indices are 0-based in Python).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
import os.path

file_name = input("Enter the file name: ")

def write_to(file_obj):
    while True:
        line = input()
        if line:
            file_obj.write("{}\n".format(line)) 
        else:
            break
   
if os.path.exists(file_name) and os.path.isfile(file_name):
    with open(file_name, 'a') as f:
        write_to(f)
else:
    with open(file_name, 'w') as f:
        write_to(f)

 
I didn't test it. No lists, no indexing. It just grab the input line and write it to a file
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
By using >= in line
while len(list_) >= counter:
you are trying to acces list_[len(list_)]. A list a with length n has elements a[0], a[1], .... a[n-1], so using a[n] raises an error.

Use just >, or better, iterate directly over your list with:
for row in list_:
    do stuff with your row
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing into 2 text files from the same function paul18fr 4 1,671 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Using .hdf5 files only once they are finished writing pyhill00 7 2,787 Nov-25-2021, 06:01 PM
Last Post: pyhill00
  Reading and writing files JakeHoward4 1 1,810 Aug-07-2019, 06:22 PM
Last Post: Yoriz
  Need help with writing to and reading from files? birdieman 14 10,577 Dec-20-2016, 10:05 PM
Last Post: birdieman

Forum Jump:

User Panel Messages

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