python - How to store words read from a file into a list -
apologies basic question new programming language. found similar questions cannot making them work specific case.
my aim read words txt file (with more lines, each word separated space), store them list , print list check doing.
the following code seems work in terms of printing single words, apparently not storing them (or not able access list).
import os def main(): read_text(dir_path) def read_text(file_name): file_data = [] text_file = open(file_name,"r") word in text_file.read().split(): print(word) file_data.append(word) text_file.close() return file_data if __name__ == "__main__": main()
what doing wrong? suggestions.
if want store data list further use, need retrieve list read_text
method returns:
def main(): resultlist = read_text(dir_path) # store list # use it...
Comments
Post a Comment