Home > database >  How to read multiple text files as string in python?
How to read multiple text files as string in python?

Time:12-12

I am writing a simple code which is working absolutely fine with one file but I want to do the same process for all files in current working folder. Just like we give * in terminal. For example

ls * //which list all the files and folders in current working directory

what I am trying to do is that I have 100s of files with some of the data I want and lots of junk data. All files are in same format just values I want are different (for your reference this files are browser request/response body saved using BurpSuite(intruder)). So all files are almost same but only some values are different according to the User Account which I want. I have deadline for this work thats why I am trying to get immediate solution instead of research more on internet and do it on own. Please help!

My code

start = "#patient_name').val('"
end = "');$('#father_name"
filename = input("Enter file name: ")
myfile = open(filename, "rt")
text = myfile.read()
print(text[text.find(start) len(start):text.rfind(end)])

My Output

Console Output

My Output!

CodePudding user response:

Create a function and iterate over file names?

import pathlib

def extract_text(filename):
    start = "#patient_name').val('"
    end = "');$('#father_name"
    with open(filename, 'rt') as myfile:
        text = myfile.read()
        return text[text.find(start) len(start):text.rfind(end)]

patients = []
for filename in pathlib.Path('.').glob('*.txt'):
    patients.append(extract_text(filename))

CodePudding user response:

If I understand what you're asking, you want to be able to accept a directory name instead of a file name and process that, processing each file in the directory one at a time. If this is correct, you can use the following method from the os library:

os.listdir(path='.') Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.' and '..' even if they are present in the directory. If a file is removed from or added to the directory during the call of this function, whether a name for that file be included is unspecified.

path may be a path-like object. If path is of type bytes (directly or indirectly through the PathLike interface), the filenames returned will also be of type bytes; in all other circumstances, they will be of type str.

This function can also support specifying a file descriptor; the file descriptor must refer to a directory.

Raises an auditing event os.listdir with argument path.

You could then ask the user for an input name, and check with the os.path.isdir() method whether the inputted name is a directory or not. If it is, you would iterate over the list returned by the os.listdir() call and do your same steps for each file name in the list. If the inputted name is not a directory, you would process it like you already do.

As a side note, since you accept an inputted file name from a user, you should either check if the inputted name exists before trying to open it (using os.path.exists()) or put your open inside a try-except block.

  • Related