Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file search by other criteria #88

Open
victordomingos opened this issue Sep 18, 2018 · 4 comments
Open

Add file search by other criteria #88

victordomingos opened this issue Sep 18, 2018 · 4 comments

Comments

@victordomingos
Copy link
Owner

victordomingos commented Sep 18, 2018

Add file search by other criteria:

  • date last modified;
  • name starts with (name prefix);
  • name ends with (name suffix);
  • name contains;
  • extension starts with (extension prefix);
  • extension ends with (extension suffix);
  • extension contains;
  • file size bigger than;
  • file size smaller than.

_Originally posted by @victordomingos in #84 (comment)

it will be difficult - many filters. can be used: os.stat, glob or pathlib
usually operating systems have tools for searching and sorting files and folders according to the pattern.
here need to think about what tasks we can solve without duplication.

Originally posted by @NataliaBondarenko in #84 (comment)

@victordomingos
Copy link
Owner Author

Actually, while there may be some shell utilities that allow this kind of functionality, sometimes its not very intuitive and requires chaining or piping some commands together. The same could be said about a considerable part of our current feature set. Having a unified tool to count and search files by those criteria may be a little less cumbersome. But we would need to build a more readable help system, as discussed in #87 .

@NataliaBondarenko
Copy link
Collaborator

NataliaBondarenko commented Sep 19, 2018

date last modified, file size bigger than, file size smaller than.
this can be applied to the list, just add more statistics from os.stat
it will be necessary to change the def show_result_for_search_files().
name starts with (name prefix); name ends with (name suffix); name contains; extension starts with (extension prefix); extension ends with (extension suffix); extension contains;
Here it is possible to make one more group: Search with regex.
with https://docs.python.org/3/library/fnmatch.html

@NataliaBondarenko
Copy link
Collaborator

something like this

from count_files.utils.file_handlers import is_hidden_file_or_dir
from typing import Iterable
import os
import fnmatch


def search_files_with_regex(dirpath: str, extension: str, recursive: bool = True,
                            include_hidden: bool = False, case_sensitive: bool = False) -> Iterable[str]:
    filter_files = fnmatch.fnmatchcase if case_sensitive else fnmatch.fnmatch
    for root, dirs, files in os.walk(dirpath):
        files = [os.path.join(root, f) for f in files
                 if os.path.isfile(os.path.join(root, f)) and filter_files(f, extension)]
        for f in files:
            if include_hidden or not is_hidden_file_or_dir(f):
                yield f
        if not recursive:
            break


# all files
a = search_files_with_regex('path', '*', True, True, True)
print(list(a))
# certain files
b = search_files_with_regex('path', '*.py', True, True, True)
print(list(b))
# certain files with case
c = search_files_with_regex('path', '*.PY', True, True, True)
print(list(c))
# certain files with condition
d = search_files_with_regex('path', '*test_*.py', True, True, True)
print(list(d))
# certain file
f = search_files_with_regex('path', 'test.py', True, True, True)
print(list(f))

@victordomingos
Copy link
Owner Author

Regex won't be for every user, but it's a nice addition. We will need to have a few practical examples in documentation.

We will have to establish the CLI arguments for these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants