From 1fb40a6540d71a57f5f45f8754567da3dcefebb2 Mon Sep 17 00:00:00 2001 From: pezy_mbp Date: Tue, 20 Jun 2017 08:29:48 +0800 Subject: [PATCH] added regex_search --- mcb.bak | 2 -- mcb.dat | Bin 1058 -> 0 bytes mcb.dir | 2 -- out_mad_libs.txt | 1 + practice_projects/mad_libs.py | 14 ++++++++++---- practice_projects/regex_search.py | 14 ++++++++++++++ 6 files changed, 25 insertions(+), 8 deletions(-) delete mode 100644 mcb.bak delete mode 100644 mcb.dat delete mode 100644 mcb.dir create mode 100644 out_mad_libs.txt create mode 100644 practice_projects/regex_search.py diff --git a/mcb.bak b/mcb.bak deleted file mode 100644 index c034909..0000000 --- a/mcb.bak +++ /dev/null @@ -1,2 +0,0 @@ -'rename', (512, 21) -'software', (1024, 34) diff --git a/mcb.dat b/mcb.dat deleted file mode 100644 index 1199a99d6377c8ea3c106e37ec77d4ce5131570f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1058 zcmZo*ju2;HV8|#bDX`MlPb*4I)k`cWC`!)LOU}>LFJ#aIN{)iz8v+f?5!^uE1*PUC z=B6ryWMmeP#`*A#ZA_m5<6i*E>bsvGR==;!Ae1WXJ7R(=?4 diff --git a/mcb.dir b/mcb.dir deleted file mode 100644 index c034909..0000000 --- a/mcb.dir +++ /dev/null @@ -1,2 +0,0 @@ -'rename', (512, 21) -'software', (1024, 34) diff --git a/out_mad_libs.txt b/out_mad_libs.txt new file mode 100644 index 0000000..69c4784 --- /dev/null +++ b/out_mad_libs.txt @@ -0,0 +1 @@ +The silly panda walked to the chandelier and then screamed. A nearby picked truck was unaffected by these events. \ No newline at end of file diff --git a/practice_projects/mad_libs.py b/practice_projects/mad_libs.py index 4dbffdd..775b74e 100644 --- a/practice_projects/mad_libs.py +++ b/practice_projects/mad_libs.py @@ -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) diff --git a/practice_projects/regex_search.py b/practice_projects/regex_search.py new file mode 100644 index 0000000..79df697 --- /dev/null +++ b/practice_projects/regex_search.py @@ -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)