Python Forum
List Won't Write in Text File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Won't Write in Text File
#2
Hello,
there are several issues with the code. First off I would start with (albeit not causing an error in this particular code) with a strong recommendation to not use Python reserved keywords and built in function names as variable names - in your case it is list (https://docs.python.org/3.8/library/stdtypes.html#list). Use a different name for the variable. I recommend using an editor with Python syntax highlighter, it will let you know of such occurrences immediately (I use Pycharm IDE, but there are sever lighter alternatives available).

While calling add_to_list() within the function itself will work, it is not recommended (that is recursion). Use a while loop instead.

list.append(add + "/n /n")
Escape character for new line is "\n" (backslash).

list_sorted = list.sort()
calling sort() on a list will sort the list itself ("list" in this case, another reason to change variable name beside previously stated - it is indescriptive). sort() (method of the list class) doesn't return anything, that is why you get "None". So either just call sort(the_list_you_want_to_sort). Or use sorted() builtin function (https://docs.python.org/3.8/library/func...tml#sorted). If you pass a list to this function, it will return the sorted variant of the list.

Assuming you would get a proper list returned, this line
list_final = str(list_sorted)
will just make this list a string of single characters. Try this in the Python console:
str(["a", "b", "c", "d"])
So don't convert the whole list to one big string. The list items are already strings, which is what you want (for outputting to file).

Recommended way to write to file is to use context manager (with). And within the context manager, use a for loop to iterate through the list of items you want to write to file. I would also recommend to append newline characters in this part of code, instead of appending to list of input strings. Example:

 with open("LP_list.txt", 'w') as output_file:
        for item in list_final:
            output_file.write(item + "\n\n")
That was a very good effort on your part. So give it another go, it's just a few changes to make and you will get it working. If you stumble upon errors or have any questions, feel free to post back.
Reply


Messages In This Thread
List Won't Write in Text File - by IILawrenceII - Jul-17-2020, 08:40 PM
RE: List Won't Write in Text File - by j.crater - Jul-17-2020, 09:31 PM
RE: List Won't Write in Text File - by IILawrenceII - Jul-17-2020, 10:45 PM
RE: List Won't Write in Text File - by j.crater - Jul-17-2020, 10:55 PM
RE: List Won't Write in Text File - by j.crater - Jul-17-2020, 11:16 PM

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 277 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 475 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,598 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,533 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,909 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,139 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,673 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,243 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,154 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  read a text file, find all integers, append to list oldtrafford 12 3,682 Aug-11-2022, 08:23 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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