Skip to main content

Helper module for locating and deleting files and folders.

Project description

Fileutility

File utility is a helper module for locating files on the local file system. It provides functionality to find files based on recursion, min-mtime, max-mtime, file suffix and timestamped filenames with regexp.

At a Glance

Installation using pip:

pip install fileutility

Example usages of fileutility:

Locate all .txt files

import fileutility

found_files = fileutility.find_files(path='/', file_suffix='.txt')

Locate all files last modified between 2017-11-30 and 2018-11-30

import fileutility
from datetime import datetime

found_files = fileutility.find_files(
    path='/', 
    minimum_file_age=datetime(2018,11,30), 
    maximum_file_age=datetime(2017,11,30)
)

Locate all .txt files with dates in filename from 2017-11-30 to 2018-11-30

import fileutility
from datetime import datetime

found_files = fileutility.find_datetime_named_files(
    path='/'
    minimum_file_age=datetime(2018,11,30),
    maximum_file_age=datetime(2017,11,30),
    file_suffix='.txt'
)

Locate all files in the local folder only

import fileutility

found_files = fileutility.find_files(
    path='/',
    recursion_depth=0
)

Delete all files in a folder and remove folder

import fileutility

found_files = fileutility.find_files(
    path='/tmp/test/'
)

delete_files(
    files=found_files,
    directory_delete=True
)

Requirements

  • Python2.7

Warning. Package has only been tested on Linux and Mac.

Supported date in filename filters from out of the box:

  • yyyy(-._)mm(-._)dd
  • yy(-._)Mon(-._)dd
  • yyyy(-._)Mon(-._)dd
  • yy(-._)mon(-._)dd
  • yyyy(-._)mon(-._)dd
  • yyyy(-._)Mon(-._)dd
  • yy(-._)Mon(-._)dd
  • yyyy(-._)mon(-._)dd
  • yy(-._)mon(-._)dd
  • yyyy-mm-ddTHH:MM:SS

If none of the above regexps provide the filter or isn't specific enough you need to apply you can always write your own:

import fileutility

found_files = fileutility.find_datetime_named_files(
    path='/'
    minimum_file_age=datetime(2018,11,30),
    maximum_file_age=datetime(2017,11,30),
    file_suffix='.txt',
    regexp=r'^myfilename-[a-z]{4,10}([0-9]{2})\@([a-z]{3})\@([0-9]{2})'
)

Unit tests status

test_find_all (__main__.TestFindDatetimeFiles) ... ok
test_find_all_suffix (__main__.TestFindDatetimeFiles) ... ok
test_find_older (__main__.TestFindDatetimeFiles) ... ok
test_find_older_younger (__main__.TestFindDatetimeFiles) ... ok
test_find_older_younger_suffix (__main__.TestFindDatetimeFiles) ... ok
test_find_regexp (__main__.TestFindDatetimeFiles) ... ok
test_find_regexp_suffix (__main__.TestFindDatetimeFiles) ... ok
test_find_regexp_suffix_no_recursion (__main__.TestFindDatetimeFiles) ... ok
test_find_root (__main__.TestFindDatetimeFiles) ... ok
test_find_subdir (__main__.TestFindDatetimeFiles) ... ok
test_find_younger (__main__.TestFindDatetimeFiles) ... ok
test_delete_files (__main__.TestFindFiles) ... ok
test_delete_files_and_directory (__main__.TestFindFiles) ... ok
test_find_all (__main__.TestFindFiles) ... ok
test_find_all_suffix (__main__.TestFindFiles) ... ok
test_find_older (__main__.TestFindFiles) ... ok
test_find_older_younger (__main__.TestFindFiles) ... ok
test_find_older_younger_suffix (__main__.TestFindFiles) ... ok
test_find_root (__main__.TestFindFiles) ... ok
test_find_subdir (__main__.TestFindFiles) ... ok
test_find_younger (__main__.TestFindFiles) ... ok

----------------------------------------------------------------------
Ran 21 tests in 0.075s

OK

Project details


Download files

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

Files for fileutility, version 0.6
Filename, size File type Python version Upload date Hashes
Filename, size fileutility-0.6-py2-none-any.whl (6.6 kB) File type Wheel Python version py2 Upload date Hashes View hashes
Filename, size fileutility-0.6.tar.gz (5.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page