Python: read all files from directory -
i trying write code read text files directory , find if file has text like:
'this color=green' 'its color=orange'
then has print specific color. code isn't printing output:
import os path = r'c:\python27' data = {} dir_entry in os.listdir(path): dir_entry_path = os.path.join(path, dir_entry) if os.path.isfile(dir_entry_path): open(dir_entry_path, 'r') my_file: data[dir_entry] = my_file.read() line in my_file: part in line.split(): if "color=" in part: print part
my output has like:
color=green color=orange
i individual files when comes directory, don't output.
as mentioned in other answers, you're reading file twice , hence pointer @ end of file.
if want data propagated contents of files printing relevant color lines, read data:
data = {} dir_item in os.listdir(path): dir_item_path = os.path.join(path, dir_item) if os.path.isfile(dir_item_path): open(dir_item_path, 'r') f: data[dir_item] = f.read() part in data[dir_item].split(): if part.startswith('color='): print part[6:]
Comments
Post a Comment