Friday, November 10, 2017

Python: Text file read and write

These are python codes for reading and writing text files:


read and store the lines into list:

def read_and_store(list_path):
with open(list_path) as f:
content = []
for line in f:
line = line.strip().split(' ')
content.append(line)
return content


write the list into a file:

def read_and_write(w_path, list_to_wr):
with open(w_path, "w") as output:
for k, val in enumerate(list_to_wr):
output.write(' '.join(val))
return None

No comments:

Post a Comment

When terminal is not starting in Ubuntu

Untitled Document.md When terminal in Ubuntu is not starting gnome-termial crashes with python 3.6 update. Go to /usr/bin/gnome-termi...