py4sci

Previous topic

regex

Next topic

checkpoint

This Page

find

Note

Functions for performing file searching.

Module author: Adam Gagorik <adam.gagorik@gmail.com>

find.depth(path, relpath)[source]

Calculate depth of path relative to relpath.

Parameters:
  • path (str) – a path
  • relpath (str) – another path
find.find(work, r=False, single=True, absolute=True, stub='*', ext=None, exclude_dirs=False, exclude_files=False, sort_by=<function numbers at 0x7fefedd48230>, at_least_one=False, follow_links=False)[source]

A method for searching for files and directories using patterns.

Parameters:
  • work (str) – path to search
  • r (bool) – perform the search recursivly
  • single (bool) – do not return a list
  • absolute (bool) – return absolute paths
  • stub (str) – the wildcard-able search pattern (star is the wildcard)
  • ext (str) – the filename extension
  • exclude_dirs (bool) – do not include directories in the search
  • exclude_files (bool) – do not include files in the search
  • sort_by (func) – a function (applied to paths) used to sort the results
  • at_least_one (bool) – make sure at least one thing was found
  • follow_links (bool) – do not follow symbolic links
>>> import os
>>> work = os.getcwd()
>>> pkls = find(work, ext='txt*') # find all txt files in work
find.systems(work, stub=None, **kwargs)[source]

Search for directories that contain “runs”.

Parameters:
>>> systems = lm.find.systems(r'/home/adam/simulations')
>>> print '\n'.join(systems)
'/home/adam/simulations/systemA'
'/home/adam/simulations/systemB'
'/home/adam/simulations/systemC'
'/home/adam/simulations/systemD'
find.sims(work, stub=None, **kwargs)[source]

Search for directories that contain “parts”.

Parameters:
>>> work = r'/home/adam/simulations/systemA/run.0'
>>> sims = lm.find.sims(work, stub='voltage.right')
>>> print '\n'.join(systems)
'/home/adam/simulations/systemA/run.0/voltage.right_+0.0'
'/home/adam/simulations/systemA/run.0/voltage.right_+0.2'
'/home/adam/simulations/systemA/run.0/voltage.right_+0.4'
'/home/adam/simulations/systemA/run.0/voltage.right_+0.8'
find.runs(*args, **kwargs)[source]

Search for directories of the form run*.

Parameters:
>>> runs = lm.find.runs(r'/home/adam/simulations/systemA/')
>>> print '\n'.join(runs)
'/home/adam/simulations/systemA/run.0/'
'/home/adam/simulations/systemA/run.1/'
'/home/adam/simulations/systemA/run.2/'
'/home/adam/simulations/systemA/run.3/'
find.run(*args, **kwargs)[source]

Search for a single directory of the form run*.

Parameters:
>>> run = lm.find.run(r'/home/adam/simulations/systemA')
RuntimeError: found multiple run* in directory:
    /home/adam/simulations/systemA
        run.0
        run.1
        run.2
        run.3
find.parts(*args, **kwargs)[source]

Search for directories of the form part*.

Parameters:
>>> prts = lm.find.parts(r'~/simulations/systemA/run.0/voltage.right_+0.0')
>>> print '\n'.join(prts)
'/home/adam/simulations/systemA/run.0/voltage.right_+0.0/part.0/'
'/home/adam/simulations/systemA/run.0/voltage.right_+0.0/part.1/'
find.part(*args, **kwargs)[source]

Search for a single directory of the form part*.

Parameters:
>>> part = lm.find.parts(r'~/simulations/systemA/run.0/voltage.right_+0.0')
RuntimeError: found multiple part* in directory:
    /home/adam/simulations/systemA/run.0/voltage.right_+0.0
        part.0
        part.1
find.inps(*args, **kwargs)[source]

Search for files of the form .inp.

Parameters:
>>> inps = lm.find.inps('.')
find.inp(*args, **kwargs)[source]

Search for a single file of the form .inp.

Parameters:
>>> inp = lm.find.inp('.')
find.chks(*args, **kwargs)[source]

Search for files of the form .chk.

Parameters:
>>> chks = lm.find.chks('.')
find.chk(*args, **kwargs)[source]

Search for a single file of the form .chk.

Parameters:
>>> chk = lm.find.chk('.')
find.parms(*args, **kwargs)[source]

Search for files of the form .parm.

Parameters:
>>> parms = lm.find.parms('.')
find.parm(*args, **kwargs)[source]

Search for a single file of the form .parm.

Parameters:
>>> parm = lm.find.parm('.')
find.dats(*args, **kwargs)[source]

Search for files of the form .dat.

Parameters:
>>> dats = lm.find.dats('.')
find.dat(*args, **kwargs)[source]

Search for a single file of the form .dat.

Parameters:
>>> dat = lm.find.dat('.')
find.txts(*args, **kwargs)[source]

Search for files of the form .txt.

Parameters:
>>> txts = lm.find.txts('.')
find.txt(*args, **kwargs)[source]

Search for a single file of the form .txt.

Parameters:
>>> txt = lm.find.txt('.')
find.pkls(*args, **kwargs)[source]

Search for files of the form .pkl.

Parameters:
>>> pkls = lm.find.pkls('.')
find.pkl(*args, **kwargs)[source]

Search for a single file of the form .pkl.

Parameters:
>>> pkl = lm.find.pkl('.')
find.pngs(*args, **kwargs)[source]

Search for files of the form .png.

Parameters:
>>> pngs = lm.find.pngs('.')
find.png(*args, **kwargs)[source]

Search for a single file of the form .png.

Parameters:
>>> png = lm.find.png('.')
find.slice_path(path, regex)[source]

Return dirname of path where the regex matches.

Parameters:
  • path (str) – path to parse
  • regex (str) – regex to match
>>> print slice_path('r/home/adam/Desktop', 'adam')
'/home/adam'
find.slice_part(path)[source]

Return dirname of path where run directory is.

Parameters:path (str) – path to parse
>>> print slice_path('r/system/run/sim/part')
'/system/run/sim/part'
find.slice_sim(path)[source]

Return dirname of path where sim directory is.

Parameters:path (str) – path to parse
>>> print slice_path('r/system/run/sim/part')
'/system/run/sim'
find.slice_run(path)[source]

Return dirname of path where run directory is.

Parameters:path (str) – path to parse
>>> print slice_path('r/system/run/sim/part')
'/system/run'
find.slice_system(path)[source]

Return dirname of path where system directory is.

Parameters:path (str) – path to parse
>>> print slice_path('r/system/run/sim/part')
'/system'
find.extract_system(path)[source]

Extract system name from path.

Parameters:path (str) – path to parse