file_tools – File names, file handlers, et al.

Module containing file manipulation functions

pyhetdex.tools.files.file_tools.prefix_filename(path, prefix)[source]

Split the file name from its path, prepend the prefix to the file name and join it back

Parameters:

path : string

file path and name

prefix : string

string to prepend

Returns:

string

path with the new file name

Examples

>>> prefix_filename('/path/to/file.dat', 'new_')
'/path/to/new_file.dat'
pyhetdex.tools.files.file_tools.scan_dirs(path, matches='*', exclude=None, recursive=True, followlinks=True)[source]

Generator that searches for and serves directories

Parameters:

path : string

path to search

matches : string or list of strings, optional

Unix shell-style wildcards to filter

exclude : string or list of strings, optional

Unix shell-style wildcards to exclude directories

recursive : bool, optional

search files recursively into path

followlinks : bool, optional

follow symlinks

Returns:

dirname : string

name of the directory (it’s an iterator, not a return)

pyhetdex.tools.files.file_tools.scan_files(path, matches='*', exclude=None, exclude_dirs=None, recursive=True, followlinks=True)[source]

Generator that search and serves files.

Parameters:

path : string

path to search

matches : string or list of strings, optional

Unix shell-style wildcards to filter; done on the full path+filename

exclude : string or list of strings, optional

Unix shell-style wildcards to exclude files; done on the full path+filename

exclude_dirs : string or list of strings, optional

Unix shell-style wildcards to exclude subdirectories

recursive : bool, optional

search files recursively into path

followlinks : bool, optional

follow symlinks

Returns:

fn : string

name of the file (it’s an iterator, not a return)

Todo

use Yields instead of Returns when the numpydoc 0.6 update will be available

pyhetdex.tools.files.file_tools.skip_comments(f)[source]

Skip commented lines and returns the file at the start of the first line without any

Parameters:

f : file object

Returns:

f : file object

moved to the next non comment line

pyhetdex.tools.files.file_tools.wildcards_to_regex(wildcards, re_compile=True)[source]

Convert shell wildcard to regex

If wildcards is None, a match-nothing regex is used If wildcards is a list, the resulting regex are concatenated with | (or)

Parameters:

wildcards : None, string or list of strings

shell wildcards

re_compile : bool, optional

if true compile the regex before returning

Returns:

regex : string or re.RegexObject

resulting regex

Examples

>>> wildcards_to_regex("[0-9]*fits")  
"re.compile('[0-9].*fits\\Z(?ms)', re.MULTILINE|re.DOTALL)"
>>> wildcards_to_regex("[0-9]*fits", re_compile=False)
'[0-9].*fits\\Z(?ms)'
>>> wildcards_to_regex(None, re_compile=False)
'a^'
>>> wildcards_to_regex(["[0-3]*fits", "[5-9]*fits"], re_compile=False)
'[0-3].*fits\\Z(?ms)|[5-9].*fits\\Z(?ms)'