0 votes
1 view
in Python by (47.7k points)

Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters?

file.writelines(["%s\n" % item for item in list])

It seems like there would be a standard way...

1 Answer

0 votes
by (107k points)

For writing a list to a file with Python you can use a loop:-

with open('your_file.txt', 'w') as f: 

for item in my_list: 

f.write("%s\n" % item)

Related questions

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...