1

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 these 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.

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

3
  • What is the expected Output? Commented Dec 11, 2021 at 14:13
  • To get a list of files in a directory there is the "glob" module. Commented Dec 11, 2021 at 14:16
  • This is not a bad question. However if you are not in a position to make use of the answers you are given, you need to obtain the services of a developer, ideally one that is based in your region. Commented Dec 11, 2021 at 19:48

2 Answers 2

1

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))
Sign up to request clarification or add additional context in comments.

8 Comments

Just like I said in Gary's comment. I directly ran your code but it didn't work. Maybe little bit of changing needed? I am trying to do it but still Thanks for the HELP bro!
First test if the function is working as expected. Replace the loop by print(extract_text('1.txt')). Does it work?
And if you have an error, please update your post with the output of this error.
Yes Its working fine with this code >> 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)] print(extract_text('1.txt'))
This script processes all the txt files in the current folder in bulk. You don't need to use input
|
0

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.

2 Comments

Well, its been a lot time since I stopped coding and changed my whole career from IT to Medical industry so I am unable to understand the explanation you gave. To be honest it looks smart explanation but just like I said its been a lot in coding and also I am having deadline for this, So can you plz give actual code so I can copy paste and finish my work. I feel little shy about asking directly for code! Sorryy!! And yeah thanks for your reply.
I would not advocate helping @Solo in the way they are asking. I've been sucked into these traps in the past, and they are unsustainable. You'll write some code out of kindness, and it will come back with a report "doesn't work" with no other details. Eventually you'll sink so much work into it that you'll contact the OP offline "just this once" and end up doing substantial free work that you don't have time or energy for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.