46. anet.core.fs — Common file manipulation

This module implements some useful functions on files and pathnames

46.1. Content of a file

Functions which change or read the content of a file or directory

anet.core.fs.fetch(filename[, object=False[, lock=None[, length=-1[, encoding=None]]]])

Returns the contents of a file.

filename is the pathname to a file. The file is read as a binary file and its contents is returned. If filename starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”).

  • If object is True, the contents is unpickled and returned
  • If lock is True, the file is locked before reading. The lock works systemwide and is only dependent on the absolute pathname of the file.
  • If length is an integer value greater dan 0, no more than length bytes are returned.
  • If encoding is not None, before returning, the content is decoded with this encoding

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.fetch(filename="clonepy.exe", length=5)
'MZ\x90\x00\x03'
>>>
anet.core.fs.store(filename[, data[, object=False[, pathmode=None[, lock=None[, append=False[, encoding=None]]]]]])

Stores data in filename

filename is the pathname to a file. If filename starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”).

data is:

  • bytes: they are written to filename
  • an object: if object is True, data is pickled and written to filename
  • unicode: encoding is used to encode. If encoding is None, brocade is used instead
  • fileobject: contents of this fileobject is copied to filename

If pathmode is specified, the properties of filename are set accordingly

If append is True, data is appended to filename

If lock is True, the file is locked before writing. The lock works systemwide and is only dependent on the absolute pathname of the file.

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.store("myfile.txt", "Hello World")
'Hello World'
>>>
anet.core.fs.copy(source, target[, pathmode=None[, r4=False[, s4=None[, l4=False[, docman=False]]]]])

Duplicates a file to a file or a directory. Returns target name

source is the file to copy. If it is a string is is handled as a filename (if this string starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”)), otherwise it is handled as a fileobject.

target is a filename or a directory name. If this string starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”)

If r4 is True, r4_ constructs are replaced with registry values.

If l4 is True, l4_ constructs are replaced with translations of the corresponding language codes.

If s4 is True, it is a mapping and s4_ constructs are replaced with their values in this mapping.

If pathmode is True, the resulting file receives the fitting file properties.

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.copy("euser.txt", "..")
'..\\euser.txt'
>>>
anet.core.fs.move(source, target[, pathmode=None[, r4=False[, s4=None[, l4=False]]]])

This is a copy with the same parameters. On completion, the original file is removed. Returns target name

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.move("euser.txt", "..")
'..\\euser.txt'
>>>
anet.core.fs.makedir(dirpath, pathmode=None)

Creates a directory with pathmode attribute settings. All subdirectories are created.

Example:

>>> from anet.core import fs
>>>
>>> fs.makedir("mydir", pathmode="webdir")
>>>
anet.core.fs.rmpath(path[, bottomup=False[, levels=0[, ignores=None[, rmroot=False]]]])

Deletes a file or a directory. If the path does not exists, the functions stops silently.

path is the pathname to a file or a directory. If path starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”).

If rmroot is False, a anet.core.raisers.RemoveRoot exception is raised if the function tries to remove the root directory.

If bottomup is True, the function tries to remove empty parent directories. The number of levels indicates how many levels (towards the root) will be tested for removal.

ignores is a glob pattern or a collection of glob patterns. It is used with bottomup: parent directories are removed if they only contain files matching one of the ignores

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.rmpath("myfile.txt")
>>>
anet.core.fs.emptydir(path)

path is the pathname of a directory. All files and subdirectories of this directory are erased.

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.emptydir(r'C:\Users\rphilips\Downloads\dejavu-fonts-ttf-2.26')
>>>

46.2. Searching for files and directories

anet.core.fs.searchpath(filename)

Returns the absolute pathname if filename.

First of all, the function checks if filename exists, if true, the absolute filename is returned Then all extensions from the PATHEXT environment variable are taken. This list is prepended with the empty string. Then, the functions checks the directories in the PATH environment if a filename concatenated with an extension exists. The absolute pathname is returned. If no suitable match is found, the function returns None

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.searchpath("python.exe")
'C:\\Python\\python.exe'
>>>
anet.core.fs.listdir(path, pattern=None, nameToLower=None)

Returns a triple: (absolute pathname of the directory path, subdirectories in path, filenames in path)

path is the pathname of a directory. If path starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”).

pattern is a glob pattern (or a list of glob patterns). Only files and directories with matching basenames are withheld.

nameToLower: if True, basenames are lowercased before matching, if None, depending on the platform nameToLower is set to True (on Windows) or False

anet.core.fs.yieldfiles([startdir=None[, patterns=None[, selectOnPath=None[, nameToLower=None[, wordsInBody=None[, bodyToLower=False[, mtime1=None[, mtime2=None[, isText=None[, size1=None[, size2=None[, recurse=False[, relpaths=False]]]]]]]]]]]]])

A generator which returns an iterator listing absolute filenames in the directory startdir

If startdir is None, the current working directory is used

patterns is a glob pattern (or a list of glob patterns). Only files with matching basenames are withheld.

selectOnPath is None or a callable of one argument (the absolute pathname). Only files for which this callable returns True are withheld.

nameToLower: if True, basenames are lowercased before matching, if None, depending on the platform nameToLower is set to True (on Windows) or False

wordsInBody is None or a string or an iterable of strings. Only files which contains all the words of wordsInBody are withheld.

bodyToLower: if True, the contents of the file is lowercased before checking the words of wordsInBody

mtime1: if not None it should be a datetime object and only files with a modification time newer or equal than mtime1 are withheld

mtime2: if not None it should be a datetime object and only files with a modification time older than mtime2 are withheld

isText: if None, this parameter has no effect. If True, only text files are withheld. if False, only binary files are withheld.

size1: If not None, it should be a number (augmented with ‘K’, ‘M’, ‘G’). Only files with a size greater or equal than size1 are withheld

size2: If not None, it should be a number (augmented with ‘K’, ‘M’, ‘G’). Only files with a size less than size2 are withheld

recurse: If True, the function recurses in the subdirectories

relpaths: If True, instead of absolute paths, relative paths to startdir are returned.

Example:

>>> from anet.core import fs
>>>
>>> list( fs.yieldfiles("C:\\Python", patterns="p*.exe", recurse=True) )
['C:\\Python\\python.exe', 'C:\\Python\\pythonw.exe', 'C:\\Python\\Scripts\\pygmentize.exe']
>>>
anet.core.fs.yielddirs([startdir=None[, patterns=None[, selectOnPath=None[, nameToLower=False[, mtime1=None[, mtime2=None[, recurse=False[, relpaths=False]]]]]]]])

A generator which returns an iterator listing absolute directory names in the directory startdir

If startdir is None, the current working directory is used

patterns is a glob pattern (or a list of glob patterns). Only directories with matching basenames are withheld.

selectOnPath is None or a callable of one argument (the absolute pathname). Only directories for which this callable returns True are withheld.

nameToLower: if True, basenames are lowercased before matching, if None, depending on the platform nameToLower is set to True (on Windows) or False

mtime1: if not None it should be a datetime object and only directories with a modification time newer or equal than mtime1 are withheld

mtime2: if not None it should be a datetime object and only directories with a modification time older than mtime2 are withheld

recurse: If True, the function recurses in the subdirectories

relpaths: If True, instead of absolute paths, relative paths to startdir are returned.

Example:

>>> from anet.core import fs
>>>
>>> list( fs.yielddirs("C:\\Python", patterns="D*") )
['C:\\Python\\DLLs', 'C:\\Python\\DLLs(35)', 'C:\\Python\\Doc']
>>>

46.3. Path construction

Several functions to conveniantly construct a path to a file or a directory in a filesystem

anet.core.fs.path(path1[, path2[, ...[, root=rootpath]]])

Build the absolute path from components. `/` acts as a universal path separator. The path is built relative to root. If root is omitted or empty, it is set to the current working directory. If root starts with `#`, this character is deleted and root is considered relative to registry(“scratch-dir”). The same transformation is done to path1, …

Some examples on the Windows platform:

>>>
>>> import os
>>> from anet.core import fs
>>>
>>> os.getcwd()
'C:\\Users\\rphilips'
>>>
>>> fs.path('a', 'b', 'c')
'C:\\Users\\rphilips\\a\\b\\c'
>>>
>>> fs.path('a', 'b', 'c', root="C:\\Windows")
'C:\\Windows\\a\\b\\c'
>>>
>>> fs.path('C:\\Windows', 'a', 'b', 'c')
'C:\\Windows\\a\\b\\c'
>>>
>>> fs.path(*['a', 'b', 'c'])
'C:\\Users\\rphilips\\a\\b\\c'
>>>
anet.core.fs.abspath(path)

Returns the absolute pathname of path. If path starts with `#`, this character is deleted and root is considered relative to registry(“scratch-dir”).

Some examples on the Windows platform:

>>>
>>> import os
>>> from anet.core import fs
>>>
>>> os.getcwd()
'C:\\Users\\rphilips\\info'
>>>
>>> fs.abspath("myfile")
'C:\\Users\\rphilips\\info\\myfile'
>>>
>>> fs.abspath("#myfile")
'c:\\users\\rphilips\\appdata\\local\\temp\\myfile'
>>>
anet.core.fs.relpath(origin, dest)

Returns the relative path from origin to dest If origin or dest starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”).

Some examples on the Windows platform:

>>> from anet.core import fs
>>>
>>>
>>> fs.relpath("a\\b", "a\\c")
'..\\c'
>>>
>>> fs.relpath("a\\b", "C:\\a\\c")
'..\\..\\..\\..\\..\\a\\c'
>>>
anet.core.fs.setext(path, ext)

Returns path with the file extension replaced by ext.

If ext is non-empty and does not start with ., the dot is prepended.

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.setext("myfile.pdf", ".zip")
'myfile.zip'
>>>

46.4. Nature of files

Some functions which test the nature of a given file

anet.core.fs.isPDF(path)

Checks if path is a PDF file.

returns True if path refers to an existing path which is a PDF file. Returns False otherwise. If path starts with `#`, this character is deleted and the path is considered relative to registry(“scratch-dir”).

Example:

>>>
>>> from anet.core import fs
>>> fs.isPDF("firefox.pdf")
True
>>>
anet.core.fs.istext([data=""[, filename=None[, ext=None]]])

Checks if a file contains ASCII text.

  1. If the filename is specified and its extension is known to be text or binary, this knowledge is returned (True for text, False for binary)
  2. If the filename is specified but the file does not exists, the result is False.
  3. If data is empty and the filename is specified and is the name of an existing file, data is filled with a piece of the file.
  4. If the ext is specified and it is an extension for a known text or binary file, this knowledge is returned (True for text, False for binary)
  5. The data is analysed and the result is reported: True for text, False for binary

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.istext(filename="clonepy.exe")
False
>>>

46.5. Temporary files and directories

Functions which handles temporary files and directories.

anet.core.fs.tempdir([suffix=""[, prefix="d"[, basedir=None[, subdir=None]]]])

Returns and creates a unique directory in basedir (or in basedir/subdir)

The basename of this unique directory starts with prefix and ends with suffix. If basedir is None, the function tries registry("scratch-dir") or the system temporary file directory. If subdir is not None, the directory is created in the subdirectory subdir of basedir.

Example:

>>> from anet.core import fs
>>>
>>> fs.tempdir()
'C:\\Users\\rphilips\\appdata\\local\\temp\\ddjknbq'
>>>
anet.core.fs.tempfile([suffix=""[, prefix="f"[, basedir=None[, content=None[, basename=None]]]]])

Returns a unique filename in basedir

If basename is specified, the resulting filename is the file basename in registry("scratch-dir")

Otherwise, the basename of this file starts with prefix and ends with suffix. It is created in the directory basedir (if basedir is None, basedir is replaced with registry("scratch-dir"))

Example:

>>>
>>> from anet.core import fs
>>>
>>> fs.tempfile(basedir=".", suffix=".tmp")
'C:\\Users\\rphilips\\fioamtt.tmp'
>>>

46.6. Properties of files

This module contains several functions to retrieve the properties of a given file. Most of them are used by the lower level functions in /core/python/installer.py and /core/python/checker.py.

Restrict the use of these functions to the functions documented here.

In a Brocade system, access codes and ownership are determined by general codes. These codes are called pathmodes. The defined pathmodes are:

os.SEEK_END

webfile: a regular file accessible on the web script: a regular, executable file webdir: a directory in the web documenttree webdav: a directory in the webdav tree toolcatdir: a directory within the python site-packages processdir: a directory to handle process relevant directories processfile: files in these directories tempdir: a temporary directory qtechfile: a file handled bu the qtech application qtechdir: a directory maintained by the qtech application naked: a completely visible directory

anet.core.fs.setfilestat(path, pathmode=None)
anet.core.fs.getatime(path)
anet.core.fs.getowner(path)
anet.core.fs.getctime(path)
anet.core.fs.getmtime(path)
anet.core.fs.cpmtime(source, target)
anet.core.fs.cpproperties(source, target)
anet.core.fs.treesize(dirname, recurse=True)
anet.core.fs.size(path)
anet.core.fs.md5(path)
anet.core.fs.isexecutable(path, user=None, group=None)
anet.core.fs.isreadable(path, user=None, group=None)
anet.core.fs.iswriteable(path, user=None, group=None)
anet.core.fs.issbit(path, user=None, group=None)
anet.core.fs.treeproperties(path, links=False, quoted=False)