Search code examples
pythonipglob

I am having problems reading multiple txt files


I am having a trouble when I try to read multiple text files in python. I'm trying to get only IP addresses from text files. When I try with a single text file, it works fine. However, when I copy more than one text file to the folder, I only get the output of the first file.

import os
import re
import glob



path = "file_direction"
pattern = re.compile(r'(?:(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)\.){3}(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)')
valid = []
invalid = []



for path in glob.glob(os.path.join(path, "*.txt")):
    with open(path, mode="r", encoding="utf-8") as fh:
        string = fh.readlines()


for line in string:
    line = line.rstrip()
    result = pattern.search(line)

    # valid IP addresses
    if result:
        valid.append(line)

    # invalid IP addresses
    else:
        invalid.append(line)

# displaying the IP addresses
print("Valid IPs")
print(valid)
print("Invalid IPs")
print(invalid)

Solution

  • you have a problem with indentation, your current code runs only on the last file.

    correct code:

    import os
    import re
    import glob
    
    path = "file_direction"
    pattern = re.compile(r'(?:(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)\.){3}(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)')
    valid = []
    invalid = []
    
    for path in glob.glob(os.path.join(path, "*.txt")):
        with open(path, mode="r", encoding="utf-8") as fh:
            string = fh.readlines()
    
        for line in string:
            line = line.rstrip()
            result = pattern.search(line)
    
            # valid IP addresses
            if result:
                valid.append(line)
    
            # invalid IP addresses
            else:
                invalid.append(line)
    
    # displaying the IP addresses
    print("Valid IPs")
    print(valid)
    print("Invalid IPs")
    print(invalid)