ska_file¶
API docs¶
Ska file utilities
- class ska_file.File.TempDir(*args, **kwargs)[source]¶
Create a temporary directory that gets automatically removed. Any object initialization parameters are passed through to tempfile.mkdtemp.
>>> import ska_file >>> tmpdir = ska_file.TempDir(dir='.') >>> tmpdir.name './tmpcCH_l-' >>> del tmpdir
- ska_file.File.chdir(dirname=None)[source]¶
Context manager to run block within dirname directory. The current directory is restored even if the block raises an exception.
>>> with chdir(dirname): >>> print "Directory within chdir() context:", os.getcwd() >>> print "Directory after chdir() context:", os.getcwd()
- Parameters
dirname – Directory name
- ska_file.File.get_globfiles(fileglob, minfiles=1, maxfiles=1)[source]¶
Get file(s) matching
fileglob
. If the number of matching files is less than minfiles or more than maxfiles then an exception is raised.- Parameters
fileglob – Input file glob
minfiles – Minimum matching files (None => no minimum)
maxfiles – Maximum matching files (None => no maximum)
- ska_file.File.make_local_copy(infile, outfile=None, copy=False, linkabs=False, clobber=True)[source]¶
Make a local copy of or link to
infile
, gunzipping if necessary.- Parameters
infile – Input file name
outfile – Output file name (default:
infile
basename)copy – Always copy instead of linking when possible
linkabs – Create link to absolute path instead of relative
clobber – Clobber existing file
- Return type
Output file name
>>> import ska_file >>> import random, tempfile >>> a = os.linesep.join([str(random.random()) for i in range(100)]) >>> tmpfile = tempfile.mkstemp()[1] >>> open(tmpfile, 'w').write(a) >>> stat = subprocess.Popen(['gzip', '--stdout', tmpfile], stdout=open(tmpfile+'.gz','w')).communicate() >>> tmplocal = ska_file.make_local_copy(tmpfile, clobber=True) >>> a == open(tmplocal).read() True >>> tmplocal = ska_file.make_local_copy(tmpfile+'.gz', clobber=True) >>> a == open(tmplocal).read() True >>> os.unlink(tmpfile) >>> os.unlink(tmplocal)
- ska_file.File.relpath(path, cwd=None)[source]¶
Find relative path from current directory to path.
Example usage:
>>> from ska_file import relpath >>> relpath('/a/b/hello/there', cwd='/a/b/c/d') '../../hello/there' >>> relpath('/a/b/c/d/e/hello/there', cwd='/a/b/c/d') 'e/hello/there'
>>> # Special case - don't go up to root and back >>> relpath('/x/y/hello/there', cwd='/a/b/c/d') '/x/y/hello/there'
- Parameters
path – Destination path
cwd – Current directory (default: os.getcwd() )
- Return type
Relative path
- ska_file.File.reversed_lines(filename)[source]¶
Generate the lines of
filename
in reverse order.Adapted from: http://stackoverflow.com/questions/260273/most-efficient-way-to-search-the-last-x-lines-of-a-file-in-python/
- Parameters
filename – file name
- Returns
generator of reversed file lines