Search code examples
pythonwindowsexplorer

Trying to find files that match the names and list their location


I am trying to create a python script that capable of autosort files that match the listing in a text file. At the moment I am trying to find the path of found file. This is written for Windows environment.

So far this is what I wrote:

import os
import sys

def find_files(filename, search_path):
   result = []

   # Walking top-down from the root
   for root, dir, files in os.walk(search_path):
      if filename in files:
         result.append(os.path.join(root, filename))
   return result

# Get the file path from the command-line arguments
if len(sys.argv) != 2:
    print("Usage: python read_file.py [file_path]")
    sys.exit(1)

file_path = sys.argv[1]

# using basename function from os
# module to print file name
tfile_fname = os.path.basename(file_path)
tfile_name = os.path.splitext(tfile_fname)

# Open the file and read its contents
with open(file_path, 'r') as f:
    contents = f.read()

# Print the file contents
print(contents)
print(tfile_fname)
print(tfile_name[0])

# Read current directory
cwd = os.getcwd()
print(cwd)

# Create a dirctory
dir = os.path.join(cwd,tfile_name[0])
if not os.path.exists(dir):
    os.mkdir(dir)
    print("Created the folder " + tfile_name[0])
else: print("The folder " + tfile_name[0] + " exists")

# Find file
for file_name in contents:
    print(find_files(contents[file_name],cwd))

The output shows this when I run the script. It tell me that the string indicies must be integers, what does that meant?

3m3
3m4
3m5
3m6
3m7
3m8
3m9

3m_test.txt
3m_test
E:\lol\test\3m
The folder 3m_test exists
Traceback (most recent call last):
  File "E:\lol\test\3m\1sort_folder.py", line 52, in <module>
    print(find_files(contents[file_name],cwd))
TypeError: string indices must be integers

Solution

  • I encourage you to take a look at the pathlib module that can do a lot of the work for you.

    import pathlib
    import sys
    
    def find_files(pattern, search_path):
        yield from (
            str(p.relative_to(search_path))
            for p
            in pathlib.Path(search_path).rglob(f"*{pattern}*")
        )
    
    ## ------------------------
    ## Get the file path from the command-line arguments
    ## ------------------------
    if len(sys.argv) != 2:
        print("Usage: python read_file.py [file_path]")
        sys.exit(1)
    file_path = sys.argv[1]
    ## ------------------------
    
    ## ------------------------
    ## Read the contents of the file that specifies 
    ## ------------------------
    with open(file_path, 'r') as file_in:
        patterns = [pattern.strip() for pattern in file_in.readlines()]
    print(f"Searching for files matching: { ' | '.join(patterns) }")
    ## ------------------------
    
    ## ------------------------
    ## Build up a list of files that match one or more
    ## pattern
    ## ------------------------
    matches = []
    for pattern in patterns:
        matches.extend(find_files(pattern, "."))
    ## ------------------------
    
    ## ------------------------
    ## Print out the relative path to files found
    ## ------------------------
    print("Matches Found:")
    for match in sorted(set(matches)):
        print(f"\t{ match }")
    ## ------------------------