Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does this code work?
#1
readfile1 = open("file.txt", "r")
f = readfile1.readlines()

newList = []

for line in f:
  newList.append(line.strip())

print(newList)
Yoriz write Apr-14-2022, 05:23 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
First, you need to have a reference or your learning will be very slow. Here is the documentation for Python: https://docs.python.org/3/

Now, answering your specific question -
First line opens the file "file.txt" for reading and associates that file with the variable "readfile1"
Next line reads the contents of that file as a list of lines, assigns that list the name f
Next you create a new empty list called newList (btw - that variable name is called CamelCase and is frowned upon in Python. Better to use new_list)
Then you start a loop, through each item in the list f you strip the whitespace off the item and append it to the new list
Finally you print the new list.
Reply
#3
It could also be made shorter.

# read file as string
readfile1 = open("file.txt", "r").read()
# split lines into list
newList = readfile1.splitlines()
#print list
print(newList)
or

newList = open("file.txt", "r").read().splitlines()
print(newList)
Reply
#4
Oder auch so?

All in one fell swoop!

@pd_minh12: if you use with open(myfile) as any_name_here: you don't need to worry about closing the file afterwards, it will be closed automatically!

myfile = '/home/pedro/summer2022/breakingnewsEnglish/heat_or_eat_UK.txt'

with open(myfile) as mf:
    newList = mf.read().splitlines()    
    for l in newList:
        print(l)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 776 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 678 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 818 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,781 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,442 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 886 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,233 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,436 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  What should i do, for this code to work -> description hamad 2 1,459 Nov-18-2021, 01:22 PM
Last Post: ghoul
  I have two Same Code but One of them Doesnt Work beginner721 6 3,085 Jan-22-2021, 10:56 PM
Last Post: beginner721

Forum Jump:

User Panel Messages

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