Skip to main content

Search for a string in all possible files in the directory.

Project description

Finder-in-files

This script searches for a specific string across all files in the directory you specified

Supported file formats for searching

  • txt
  • log
  • html
  • css
  • cpp
  • h
  • js
  • py
  • c

Special formats

  • doc
  • docx
  • otd
  • rtf (May not work on Windows)
  • pdf (May not work on Windows)

Documentation

Installation

pip install finder-string==0.1.1

Import

To import a module into your code use:

import finder_in_files as finder

Usage

To find a line in files in a directory, use:

finder.search(catalog_name, find_str, settings_files=DEFAULT_SETTINGS_FILES)

catalog_name - The name of the directory where the search should be performed.

find_str - Search string.

settings_files - Setting file extensions (enable / disable).

DEFAULT_SETTINGS_FILES -

DEFAULT_SETTINGS_FILES = {
       'txt': True,
       'log': True,
       'html': True,
       'css': True,
       'cpp': True,
       'h': True,
       'py': True,
       'c': True,
       'doc': True,
       'docx': True,
       'rtf': True,
       'odt': True,
       'pdf': True
}

The search is performed on the lines of the file. If find_str is more than 89% similar to a line in the file, the line is considered found.

The search function returns a tuple (name_file, numbers_str numbers_repeat).

name_file - The path to the file where find_str was found.

numbers_str - Returned as a list that consists of the lines where find_str was found.

numbers_repeat - The number of times to repeat find_str in the file. Returned in int format.

Example

An example where all of this is used together.

try:
                results = finder_in_files.search(catalog_name, find_str, self.settings_files)
                if not results:  # If nothing was found, then
                    print('\nThe text was not found. ;(\n')
                else:
                    for name_file, numbers_str, numbers_repeat in results:  # We unpack the tuple using for.
                        print(f"\nPath to the file: {name_file}")
                        print(f'Content lines: {str(numbers_str).replace(",", " |")}')
                        print(f"String repetitions: {numbers_repeat}\n")
            except FileNotFoundError:  # print('\nError! You entered an invalid file path.')  
            # An exception if the path (catalog_name) was not specified correctly.

More examples on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

finder_string-0.1.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

finder_string-0.1.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file finder_string-0.1.1.tar.gz.

File metadata

  • Download URL: finder_string-0.1.1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for finder_string-0.1.1.tar.gz
Algorithm Hash digest
SHA256 23449a0d73db7c88c41772e0c34397be876600a4688998d99271efa8008763ac
MD5 533c0e62ce1415f69654bed24399a66e
BLAKE2b-256 865b7f07b443e8c846630f5ab774f7218badd5c45e0bda953e6a3d5112baf0b4

See more details on using hashes here.

File details

Details for the file finder_string-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: finder_string-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for finder_string-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7ad4a55bf1e50a57f56ca785cc5f9dd4b04c059e9fa684a93bb9238f1d9bfcf
MD5 70fe6444b974201dc5b1cceaf4d1d1b4
BLAKE2b-256 c01e6db0d194105d07815df009bb91242c9fe95100a2db150d9b3ddd10429a70

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page