Answers for "python add a list to file txt"

2

how to put a text file into a list python

# name.txt
david
mary
john


with open('names.txt', 'r') as f:
    myNames = [line.strip() for line in f]
    
# Result

['david','mary','john']
Posted by: Guest on December-08-2020
0

append file to list python

list=[]
f = open('file.txt','r')

for line in f:
    list.append(line.rstrip())  #if you want the n replace rstrip by strip
Posted by: Guest on November-10-2021

Code answers related to "python add a list to file txt"

Python Answers by Framework

Browse Popular Code Answers by Language