I have this code that sorts a document according to the words it contains. There is a pythonic way of counting the files classified as "_fruit" and "_veget" that I am moving to the folder untitled1
path_txt = 'temp'
files1 = os.listdir(path_txt)
for TXT in files1:
with open(path_txt + '\\' + TXT, "r") as content:
key1 = False
key2 = False
search = re.search(r'\d{21,30}', content.read())
text = content.read()
if ('apple' in text and 'banana' in text and 'orange') or \
('apple' in text and 'orange' in text and 'watermelon'):
key1 = True
elif ('pumpkin' in text or 'potato' in text) and \
'carrot' in text and '' in text and \
'beet' in text and not 'pineapple' in text and not 'orange' in text:
key2 = True
if search is not None:
name1 = search.group(0)
if key1:
fp = os.path.join("untitled1", name1 + '_fruit' + "_%d.txt")
postfix = 0
while os.path.exists(fp % postfix):
postfix += 1
elif key2:
fp = os.path.join("untitled1", name1 + '_veget' + "_%d.txt")
postfix = 0
while os.path.exists(fp % postfix):
postfix += 1
os.rename(
os.path.join(path_txt, TXT),
fp % postfix
)ps: I have 8 "for" loops in the same code doing similar things. I will apply for them as well.
