Skip to main content

Search for files by the specified path.

Project description

SearchFile

Table of contents

  1. Installation
  2. Description
  3. Using
  4. Code

Installation

Installation using a terminal:

pip install SearchFile

Description

This library searches for files by the specified parameters.

Using

import SearchFile

or

from SearchFile import search

The search function accepts three parameters.

tpath: directory path

name_parameter: file name or the first match of a substring with the name

extension_parameter: file extension

returns a list sorted by the specified parameters

For Windows

Also, the directory separator should be written in the form \\, so as not to form control characters (\n, \t, \r, etc.). Examples:

search("C:\\Users")
search("D:\\Downloads\\test.txt", "t")
search("D:\\Git\\cmd", "t", ".txt")
search("C:\\Users", extension_parameter=".txt")

Linux

The directory separator should be used /. Examples:

search("/home/artem/desktop/tmp/")
search("/home/artem/desktop/", "t")
search("/home/artem/tmp/file1", "f", ".txt")
search("/home/artem/desktop/tmp/", extension_parameter=".txt")

Mac OS

The directory separator should be used /. Examples:

search("/Library/Desktop Pictures")
search("/Library/Desktop Pictures/pictures1.jpg", "pictures1")
search("/Library/Desktop Pictures/gallery", "f", ".jpg")
search("/Library/Desktop Pictures", extension_parameter=".png")

Code

from os import path, listdir


def search(tpath: str, name_parameter: str = None, extension_parameter: str = None) -> list:
    list_all_files = []

    def search_all_files(transmission_path):
        if path.isdir(transmission_path):
            for dirs in listdir(transmission_path):
                if path.isdir(path.join(transmission_path, dirs)):
                    search_all_files(path.join(transmission_path, dirs))
                else:
                    list_all_files.append(path.basename(path.join(transmission_path, dirs)))
            return list_all_files
        else:
            if path.isfile(transmission_path):
                list_all_files.append(path.basename(transmission_path))
            else:
                return "The path is specified incorrectly!"
            return list_all_files

    if name_parameter is not None:
        list_needed_files = [files for files in search_all_files(tpath)
                             if files.find(name_parameter) == 0]
        if extension_parameter is not None:
            list_needed_files = [files for files in list_needed_files
                                 if files.rfind(extension_parameter) == files.rfind(".")]
        return list_needed_files

    if extension_parameter is not None:
        list_needed_files = [files for files in search_all_files(tpath)
                             if files.rfind(extension_parameter) == files.rfind(".")]
        return list_needed_files

    return search_all_files(tpath)

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

SearchFile-1.0.0.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

SearchFile-1.0.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file SearchFile-1.0.0.tar.gz.

File metadata

  • Download URL: SearchFile-1.0.0.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.0

File hashes

Hashes for SearchFile-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1caccf19bf65e5279bfcb40411df8c60dae989bd345dcbcddd6b7a153259d607
MD5 a0a49fe4a949ab2810c584de9f8d6700
BLAKE2b-256 de9e82cb4cc67e32f33959266a96e99d72eb9a4cfdbab046b500951d2692bdee

See more details on using hashes here.

File details

Details for the file SearchFile-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: SearchFile-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.0

File hashes

Hashes for SearchFile-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4bae5eaefc7c4174722549a5c95940d983f2a2e8628093148b6403c9562bd320
MD5 545ffb8acbf0ba91805acfd52a76dc5d
BLAKE2b-256 7fb78b108c1412d263af66a6ba0417dc18bf6689e9d58c6af30ea990c95dafc7

See more details on using hashes here.

Supported by

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