0
votes

I am trying to find specific strings in a lot of files. For example I am looking for files which contains the string "hello" in a directory and subdirectores.

Lets say my directory system looks like this: File1 and File3 from Dir1 contains the string "hello" while File2 don't. In Dir2 File1 contains the string "hello".

MainDir:
 -> Dir1
    -> SubDir1
       ->File1
       ->File2
    -> SubDir2
       ->File3
 -> Dir2 
    ->SubDir1
      ->File1
      ->SubDir1.1
        ->SubDir1.1.1
          -> File1
          -> File2

My code:

 path = "C:\MainDir" #I also get error if I write C:\MainDir\Dir2\SubDir1
 word = "hello"
 for root, dirs, files in os.walk(path):
    for name in files:
        if name.endswith(".txt"):
            with open(os.path.join(path, name)) as fle:
                my_files_content = fle.read()
            if word in my_files_content:
                print fle.name

If i write the full path if will find the files which contains the string "hello" (for example: path = "C:\MainDir\SubDir1" or C:\MainDir\Dir2\SubDir1.1\SubDir1.1) But if I write just the path as in my code it will give me an error "No such file or directory:"

3
Not sure if you directly copied and pasted your code but you're missing a closing " on your path name. - zephyr
I suspect that you directly write something like "c:\foo\bar.txt", and have \f and \b interpreted as escape sequences. You can duplucate backslashes ("c:\\foo\\bar.txt") or use raw strings (r"c:\foo\bar.txt") to avoid that. - 9000
I don't think that is the issue though. The code is not able to go into the subdirectories and look for the files in there. - user825023

3 Answers

1
votes

The problem is that path is a variable that you set, so it won't add in the sub directory to the path. So instead of open(os.path.join(path, name)) you should do open(os.path.join(root, name)).

1
votes

I fixed your code and test good in my system:

    import os
    paths = r"C:\MainDir" #I also get error if I write C:\MainDir\Dir2\SubDir1
    word = "hello"
    for root, dirs, files in os.walk(paths):
        for name in files:
            if name.lower().endswith(".txt"):
                with open(os.path.join(root, name)) as fle:
                    my_files_content = fle.read()
                    if word in my_files_content:
                        print fle.name

You have joined a wrong path,here is a tutorial link os.walk.

-3
votes

the thing with this is you'll come across a lot of different types of files, and python cant just open any file, as it needs admin for half, or like it is unable to gain access like the recycling bin, so the question is would you need to limit the files, so you only open '.txt' files? of course i have a code that can do an enormous search, and list all the items in my directory, (use this in case of malware I would like to find) but i have never got it to open a bunch of different types of files...

so i have now edited my code to do a search on anyone's pc downloads, all you have to do is copy this code exact and run it, and it should print all the directories of your downloads (if your running a windows though).

import os
folder_path = (os.path.expanduser('~\\Downloads'))
for file_object in os.listdir(folder_path):
    file_object_path = os.path.join(folder_path, file_object)
    if os.path.isfile(file_object_path):
        print (file_object_path)
    else:
        print (file_object_path)