Package pythonutils :: Module pathutils
[hide private]
[frames] | no frames]

Module pathutils
source code

This module contains convenience functions for working with files and paths.

Classes [hide private]
LockError The generic error for locking - it is a subclass of IOError.
Lock A simple file lock, compatible with windows and Unixes.
LockFile A file like object with an exclusive lock, whilst it is open.

Functions [hide private]
  readlines(filename)
Passed a filename, it reads it, and returns a list of lines.
  writelines(filename, infile, newline=False)
Given a filename and a list of lines it writes the file.
  readbinary(filename)
Given a filename, read a file in binary mode.
  writebinary(filename, infile)
Given a filename and a string, write the file in binary mode.
  readfile(filename)
Given a filename, read a file in text mode.
  writefile(filename, infile)
Given a filename and a string, write the file in text mode.
  tslash(apath)
Add a trailing slash (/) to a path if it lacks one.
  relpath(origin, dest)
Return the relative path between origin and dest.
  splitall(loc)
Return a list of the path components in loc.
  walkfiles(D)
Yields full file paths.
  walkdirs(thisdir)
Walk through all the subdirectories in a tree.
  walkemptydirs(thisdir)
Recursively yield names of empty directories.
  formatbytes(sizeint, configdict=None, **configs)
Given a file size as an integer, return a nicely formatted string that represents the size.
  stringround(main, rest)
Given a file size in either (mb, kb) or (kb, bytes) - round it appropriately.
  bytedivider(nbytes)
Given an integer (probably a long integer returned by os.getsize() ) it returns a tuple of (megabytes, kilobytes, bytes).
  fullcopy(src, dst)
Copy file from src to dst.
  import_path(fullpath, strict=True)
Import a file from the full path.
  main_is_frozen()
Return True if we're running from a frozen program.
  get_main_dir()
Return the script directory - whether we're frozen or not.
  onerror(func, path, exc_info)
Error handler for shutil.rmtree.

Variables [hide private]
__version__  
join  
isdir  
isfile  
generators  

Imports: os, sys, time

Function Details [hide private]

readlines(filename)

source code 
Passed a filename, it reads it, and returns a list of lines. (Read in text mode)

writelines(filename, infile, newline=False)

source code 

Given a filename and a list of lines it writes the file. (In text mode)

If newline is True (default is False) it adds a newline to each line.

readbinary(filename)

source code 
Given a filename, read a file in binary mode. It returns a single string.

writebinary(filename, infile)

source code 
Given a filename and a string, write the file in binary mode.

readfile(filename)

source code 
Given a filename, read a file in text mode. It returns a single string.

writefile(filename, infile)

source code 
Given a filename and a string, write the file in text mode.

tslash(apath)

source code 

Add a trailing slash (/) to a path if it lacks one.

It doesn't use os.sep because you end up in trouble on windoze, when you want separators for URLs.

relpath(origin, dest)

source code 

Return the relative path between origin and dest.

If it's not possible return dest.

If they are identical return os.curdir

Adapted from path.py by Jason Orendorff.

splitall(loc)

source code 

Return a list of the path components in loc. (Used by relpath_).

The first item in the list will be  either ``os.curdir``, ``os.pardir``, empty,
or the root directory of loc (for example, ``/`` or ``C:\).

The other items in the list will be strings.
    
Adapted from *path.py* by Jason Orendorff.

walkfiles(D)

source code 

Yields full file paths.

Adapted from path.py by Jason Orendorff.

Returns:
iterator over files in D, recursively

walkdirs(thisdir)

source code 
Walk through all the subdirectories in a tree. Recursively yields directory names (full paths).

walkemptydirs(thisdir)

source code 

Recursively yield names of empty directories.

These are the only paths omitted when using walkfiles.

formatbytes(sizeint, configdict=None, **configs)

source code 

Given a file size as an integer, return a nicely formatted string that represents the size. Has various options to control it's output.

You can pass in a dictionary of arguments or keyword arguments. Keyword arguments override the dictionary and there are sensible defaults for options you don't set.

Options and defaults are as follows :

  • forcekb = False - If set this forces the output to be in terms

of kilobytes and bytes only.

  • largestonly = True - If set, instead of outputting
1 Mbytes, 307 Kbytes, 478 bytes it outputs using only the largest denominator - e.g. 1.3 Mbytes or 17.2 Kbytes
  • kiloname = 'Kbytes' - The string to use for kilobytes
  • meganame = 'Mbytes' - The string to use for Megabytes
  • bytename = 'bytes' - The string to use for bytes
  • nospace = True - If set it outputs 1Mbytes, 307Kbytes,
notice there is no space.

Example outputs :

19Mbytes, 75Kbytes, 255bytes
2Kbytes, 0bytes
23.8Mbytes

Note

It currently uses the plural form even for singular.

stringround(main, rest)

source code 
Given a file size in either (mb, kb) or (kb, bytes) - round it appropriately.

bytedivider(nbytes)

source code 

Given an integer (probably a long integer returned by os.getsize() ) it returns a tuple of (megabytes, kilobytes, bytes).

This can be more easily converted into a formatted string to display the size of the file.

fullcopy(src, dst)

source code 

Copy file from src to dst.

If the dst directory doesn't exist, we will attempt to create it using makedirs.

import_path(fullpath, strict=True)

source code 

Import a file from the full path. Allows you to import from anywhere, something __import__ does not do.

If strict is True (the default), raise an ImportError if the module is found in the "wrong" directory.

Taken from firedrop2 by Hans Nowak

main_is_frozen()

source code 
Return True if we're running from a frozen program.

get_main_dir()

source code 
Return the script directory - whether we're frozen or not.

onerror(func, path, exc_info)

source code 

Error handler for shutil.rmtree.

If the error is due to an access error (read only file) it attempts to add write permission and then retries.

If the error is for another reason it re-raises the error.

Usage : shutil.rmtree(path, onerror=onerror)


Variables Details [hide private]

__version__

Value:
'0.2.4'                                                                
      

join

Value:
<function join at 0x00993AF0>                                          
      

isdir

Value:
<function isdir at 0x00993E70>                                         
      

isfile

Value:
<function isfile at 0x00993EB0>                                        
      

generators

Value:
_Feature((2, 2, 0, 'alpha', 1), (2, 3, 0, 'final', 0), 4096)