Python Forum
Function combining file manipulation and loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function combining file manipulation and loop
#1
good evening,
could I ask for some help with this assignment?

Quote:We want to save a list of words in a file so that we can reuse it later.

Define a function write_list_to_file(wordlst, filename) taking as parameters a list of words wordlst and a filename filename. The function should store the words in the list in a file (UTF8 encoding), one word per line.

so far I wrote:

wordlst=['walrus','cat','parrot','fish','monkey','giraffe'] #create a list of words

def write_list_to_file(wordlst, filename): #define function
    with open("filename.txt","w") as myfile: #the file doesn't exist, so it should create one, open in write mode
        for word in wordlst: #for element in wordlst
            myfile.write(word) #write element in myfile
        print(write_list_to_file(wordlst,filename)) #nothing happens


can you please point out my mistake? also I'd like to use \n somewhere to make sure to store one word per line,maybe on third line of the function?

thanks in adavance,
Leyo
Reply
#2
Did you call the function?
Reply
#3
(Mar-22-2022, 09:39 PM)deanhystad Wrote: Did you call the function?

hello,

when calling the function:
write_list_to_file(wordlst,myfile)
I get:
Error:
NameError Traceback (most recent call last) /tmp/ipykernel_6780/3840483989.py in <module> 6 myfile.write(word) #write element in myfile 7 print(write_list_to_file(wordlst,filename)) #nothing happens ----> 8 write_list_to_file(wordlst,myfile) 9 # votre code ici NameError: name 'myfile' is not defined
Reply
#4
Hello,
You have to call the function with the name of the file as parameter.
wordlst = ['car', 'cat', 'dog']
write_list_to_file(wordlst, "filename.txt")
and modify your code like this:
def write_list_to_file(wordlst, filename): 
    with open(filename,"w") as myfile: 
        for word in wordlst: 
            myfile.write(word+"\n")
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#5
Quote of assignment doesn't mention loop. So alternative way could be to skip loop and use print (unpack list with*, set separator to newline, print to file) :

words =['walrus','cat','parrot','fish','monkey','giraffe']

def write_list_to_file(list_, filename):
    with open(filename, "w", encoding="UTF8") as f:
        print(*list_, sep='\n', file=f)

write_list_to_file(words, 'write_list_to_file.txt')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
so simple...thank you Coricoco_fr!
thank you Perfringo, great to learn alternative ways to do this
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I run a function inside a loop every 24 values of the loop iteration range? mcva 1 2,101 Sep-18-2019, 04:50 PM
Last Post: buran
  help! function that checks if a file is a bed file, tips so i can code it myself lilyS 1 2,338 May-31-2019, 12:41 PM
Last Post: ichabod801
  While loop within a Function (2.7) tuffgong 3 3,464 Jun-28-2017, 05:54 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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