pyyaks.fileutil

Pyyaks file utilities

Functions

pyyaks.fileutil.get_globfiles(fileglob, minfiles=1, maxfiles=1)

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)
pyyaks.fileutil.make_local_copy(infile, outfile=None, copy=False, linkabs=False, clobber=True)

Make a local copy of or link to infile, gunzipping if necessary.

Examples:

>>> import pyyaks.fileutil
>>> 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 = pyyaks.fileutil.make_local_copy(tmpfile, clobber=True)
>>> a == open(tmplocal).read()
True
>>> tmplocal = pyyaks.fileutil.make_local_copy(tmpfile+'.gz', clobber=True)
>>> a == open(tmplocal).read()
True
>>> os.unlink(tmpfile)
>>> os.unlink(tmplocal)
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

pyyaks.fileutil.relpath(path, cwd=None)

Find relative path from current directory to path.

Example usage:

>>> from pyyaks.fileutil 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

Classes

class pyyaks.fileutil.TempDir(*args, **kwargs)

Bases: object

Create a temporary directory that gets automatically removed. Any object initialization parameters are passed through to tempfile.mkdtemp.

>>> import pyyaks.fileutil
>>> tmpdir = pyyaks.fileutil.TempDir(dir='.')
>>> tmpdir.name
'./tmpcCH_l-'
>>> del tmpdir

Table Of Contents

Previous topic

pyyaks.context

Next topic

pyyaks.logger

This Page