Skip to main content
added 17 characters in body
Source Link
Javana
  • 451
  • 2
  • 6
import os, re
from pathlib import Path

while True:
    user_path = input("Enter a path to search: ")
    if user_path == "":
        print("Please enter a valid path.")
    elif Path(user_path).exists() != True:
        print("This path does not exist.")
    else:
        while True:
            all_files = os.listdir(user_path)
            matches = []
            lines = []
            user_inpuser_input = input("What do you want to find? (Enter nothing to search another path)\n")
            rx_objregex_object = re.compile(user_inpuser_input)
            if user_inpuser_input == "":
                break
            else:
                for filename in all_files:
                    if filename.endswith(".txt") != True:
                        continue
                    text_file = open(Path(user_path) / filename)
                    for line in text_file.readlines():
                        momatch_object = rx_objregex_object.findall(line)
                        for match in momatch_object:
                            matches.append(match)
                            lines.append(line)
                    text_file.close()
            if matches == []:
                print("No matches found")
            else:
                print(matches)
                print(lines)

I feel like I've probably overcomplicated things...

Thanks in advance for the critique.

Edit: This only works when given a folder with only .txt files.

import os, re
from pathlib import Path

while True:
    user_path = input("Enter a path to search: ")
    if user_path == "":
        print("Please enter a valid path.")
    elif Path(user_path).exists() != True:
        print("This path does not exist.")
    else:
        while True:
            all_files = os.listdir(user_path)
            matches = []
            lines = []
            user_inp = input("What do you want to find? (Enter nothing to search another path)\n")
            rx_obj = re.compile(user_inp)
            if user_inp == "":
                break
            else:
                for filename in all_files:
                    text_file = open(Path(user_path) / filename)
                    for line in text_file.readlines():
                        mo = rx_obj.findall(line)
                        for match in mo:
                            matches.append(match)
                            lines.append(line)
                    text_file.close()
            if matches == []:
                print("No matches found")
            else:
                print(matches)
                print(lines)

I feel like I've probably overcomplicated things...

Thanks in advance for the critique.

Edit: This only works when given a folder with only .txt files.

import os, re
from pathlib import Path

while True:
    user_path = input("Enter a path to search: ")
    if user_path == "":
        print("Please enter a valid path.")
    elif Path(user_path).exists() != True:
        print("This path does not exist.")
    else:
        while True:
            all_files = os.listdir(user_path)
            matches = []
            lines = []
            user_input = input("What do you want to find? (Enter nothing to search another path)\n")
            regex_object = re.compile(user_input)
            if user_input == "":
                break
            else:
                for filename in all_files:
                    if filename.endswith(".txt") != True:
                        continue
                    text_file = open(Path(user_path) / filename)
                    for line in text_file.readlines():
                        match_object = regex_object.findall(line)
                        for match in match_object:
                            matches.append(match)
                            lines.append(line)
                    text_file.close()
            if matches == []:
                print("No matches found")
            else:
                print(matches)
                print(lines)

Thanks in advance for the critique.

Post Undeleted by Javana
Post Deleted by Javana
added 67 characters in body
Source Link
Javana
  • 451
  • 2
  • 6

The project outline:

Write a program that 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.

My solution:

import os, re
from pathlib import Path

while True:
    user_path = input("Enter a path to search: ")
    if user_path == "":
        print("Please enter a valid path.")
    elif Path(user_path).exists() != True:
        print("This path does not exist.")
    else:
        while True:
            all_files = os.listdir(user_path)
            matches = []
            lines = []
            user_inp = input("What do you want to find? (Enter nothing to search another path)\n")
            rx_obj = re.compile(user_inp)
            if user_inp == "":
                break
            else:
                for filename in all_files:
                    text_file = open(Path(user_path) / filename)
                    for line in text_file.readlines():
                        mo = rx_obj.findall(line)
                        for match in mo:
                            matches.append(match)
                            lines.append(line)
                    text_file.close()
            if matches == []:
                print("No matches found")
            else:
                print(matches)
                print(lines)

I feel like I've probably overcomplicated things...

Thanks in advance for the critique.

Edit: This only works when given a folder with only .txt files.

The project outline:

Write a program that 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.

My solution:

import os, re
from pathlib import Path

while True:
    user_path = input("Enter a path to search: ")
    if user_path == "":
        print("Please enter a valid path.")
    elif Path(user_path).exists() != True:
        print("This path does not exist.")
    else:
        while True:
            all_files = os.listdir(user_path)
            matches = []
            lines = []
            user_inp = input("What do you want to find? (Enter nothing to search another path)\n")
            rx_obj = re.compile(user_inp)
            if user_inp == "":
                break
            else:
                for filename in all_files:
                    text_file = open(Path(user_path) / filename)
                    for line in text_file.readlines():
                        mo = rx_obj.findall(line)
                        for match in mo:
                            matches.append(match)
                            lines.append(line)
                    text_file.close()
            if matches == []:
                print("No matches found")
            else:
                print(matches)
                print(lines)

I feel like I've probably overcomplicated things...

Thanks in advance for the critique.

The project outline:

Write a program that 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.

My solution:

import os, re
from pathlib import Path

while True:
    user_path = input("Enter a path to search: ")
    if user_path == "":
        print("Please enter a valid path.")
    elif Path(user_path).exists() != True:
        print("This path does not exist.")
    else:
        while True:
            all_files = os.listdir(user_path)
            matches = []
            lines = []
            user_inp = input("What do you want to find? (Enter nothing to search another path)\n")
            rx_obj = re.compile(user_inp)
            if user_inp == "":
                break
            else:
                for filename in all_files:
                    text_file = open(Path(user_path) / filename)
                    for line in text_file.readlines():
                        mo = rx_obj.findall(line)
                        for match in mo:
                            matches.append(match)
                            lines.append(line)
                    text_file.close()
            if matches == []:
                print("No matches found")
            else:
                print(matches)
                print(lines)

I feel like I've probably overcomplicated things...

Thanks in advance for the critique.

Edit: This only works when given a folder with only .txt files.

Source Link
Javana
  • 451
  • 2
  • 6

Automate the Boring Stuff Chapter 9 - Regex Search

The project outline:

Write a program that 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.

My solution:

import os, re
from pathlib import Path

while True:
    user_path = input("Enter a path to search: ")
    if user_path == "":
        print("Please enter a valid path.")
    elif Path(user_path).exists() != True:
        print("This path does not exist.")
    else:
        while True:
            all_files = os.listdir(user_path)
            matches = []
            lines = []
            user_inp = input("What do you want to find? (Enter nothing to search another path)\n")
            rx_obj = re.compile(user_inp)
            if user_inp == "":
                break
            else:
                for filename in all_files:
                    text_file = open(Path(user_path) / filename)
                    for line in text_file.readlines():
                        mo = rx_obj.findall(line)
                        for match in mo:
                            matches.append(match)
                            lines.append(line)
                    text_file.close()
            if matches == []:
                print("No matches found")
            else:
                print(matches)
                print(lines)

I feel like I've probably overcomplicated things...

Thanks in advance for the critique.