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
#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


Messages In This Thread
RE: Function combining file manipulation and loop - by perfringo - Mar-23-2022, 08:47 AM

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,165 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,403 May-31-2019, 12:41 PM
Last Post: ichabod801
  While loop within a Function (2.7) tuffgong 3 3,557 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