added regex_search

This commit is contained in:
pezy_mbp
2017-06-20 08:29:48 +08:00
parent cb1401d9f7
commit 1fb40a6540
6 changed files with 25 additions and 8 deletions

View File

@@ -1,2 +0,0 @@
'rename', (512, 21)
'software', (1024, 34)

BIN
mcb.dat

Binary file not shown.

View File

@@ -1,2 +0,0 @@
'rename', (512, 21)
'software', (1024, 34)

1
out_mad_libs.txt Normal file
View File

@@ -0,0 +1 @@
The silly panda walked to the chandelier and then screamed. A nearby picked truck was unaffected by these events.

View File

@@ -31,9 +31,15 @@ mad_libs.py - reads in text files and lets the user add their own text anywhere
import re
file_name = input("Enter the filename:")
file_read = open(file_name)
content = file_read.read()
in_file_name = input("Enter the filename:\n")
in_file = open(in_file_name)
content = in_file.read()
for to_replace in re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB').findall(content):
content.replace(to_replace)
str_input = input("Enter %s %s:\n" % ('an' if to_replace[0] in 'AEIOU' else
'a', to_replace.lower()))
content = content.replace(to_replace, str_input, 1)
out_file_name = input("Enter the output file name:\n")
out_file = open(out_file_name, 'w')
out_file.write(content)

View File

@@ -0,0 +1,14 @@
#! python3
'''
regex_search.py - opens all .txt files in a folder and searches for any line
that matches a user-supplied regular expression. The results
should be printed to the screen.
'''
import os
target_folder = input("Enter the target foulder:\n")
os.chdir(target_folder)
target_files = os.listdir(target_folder)
target_files = [target for target in target_files if target.endswith('.txt')]
print(target_files)