3.3. Files and Directories

3.3.1. Introduction

The file managment API provides the ability to interact with (local and remote) file systems via the two classes, saga.filesystem.Directory and saga.filesystem.File. The API provides a number of operations, which all behave similar to the common unix command line tools (cp, ls, rm etc).

Example:

# get a directory handle
dir = saga.filesystem.Directory("sftp://localhost/tmp/")

# create a subdir
dir.make_dir ("data/")

# list contents of the directory
files = dir.list ()

# copy *.dat files into the subdir
for f in files :
    if re.match ('^.*\.dat$', f) :
        dir.copy (f, "sftp://localhost/tmp/data/")

The above example covers most of the semantics of the filesystem package – additional capabilities, such get_size() or move(), can be found in the individual class documentations.

3.3.2. Flags

The following constants are defined as valid flags for file and directory methods:

saga.filesystem.OVERWRITE
saga.filesystem.RECURSIVE
saga.filesystem.CREATE
saga.filesystem.CREATE_PARENTS
saga.filesystem.LOCK
saga.filesystem.EXCLUSIVE
saga.filesystem.DEREFERENCE

3.3.3. File – saga.filesystem.File

class saga.filesystem.File(url=None, flags=512, session=None, _adaptor=None, _adaptor_state={}, _ttype=None)[source]

Bases: saga.namespace.entry.Entry

Represents a local or remote file.

The saga.filesystem.File class represents, as the name indicates, a file on some (local or remote) filesystem. That class offers a number of operations on that file, such as copy, move and remove:

# get a file handle
file = saga.filesystem.File("sftp://localhost/tmp/data/data.bin")

# copy the file
file.copy ("sftp://localhost/tmp/data/data.bak")

# move the file
file.move ("sftp://localhost/tmp/data/data.new")
__init__(url, flags=READ, session)[source]

Construct a new file object

Parameters:
  • url (saga.Url) – Url of the (remote) file
  • sessionsaga.Session
Fgs:

Flags

The specified file is expected to exist – otherwise a DoesNotExist exception is raised. Also, the URL must point to a file (not to a directory), otherwise a BadParameter exception is raised.

Example:

# get a file handle
file = saga.filesystem.File("sftp://localhost/tmp/data/data.bin")

# print the file's size
print file.get_size ()
is_file()[source]

Returns True if instance points to a file, False otherwise.

get_size()[source]

Returns the size (in bytes) of a file.

Example:

# get a file handle
file = saga.filesystem.File("sftp://localhost/tmp/data/data.bin")

# print the file's size
print file.get_size ()
close(kill=True, ttype=None)[source]

kill : bool ttype: saga.task.type enum ret: string / bytearray / saga.Task

size

get_size()

Returns the size (in bytes) of a file.

Example:

# get a file handle
file = saga.filesystem.File("sftp://localhost/tmp/data/data.bin")

# print the file's size
print file.get_size ()

3.3.4. Directory – saga.filesystem.Directory

class saga.filesystem.Directory(url=None, flags=512, session=None, _adaptor=None, _adaptor_state={}, _ttype=None)[source]

Bases: saga.namespace.directory.Directory

Represents a (remote) directory.

The saga.filesystem.Directory class represents, as the name indicates, a directory on some (local or remote) filesystem. That class offers a number of operations on that directory, such as listing its contents, copying files, or creating subdirectories:

# get a directory handle
dir = saga.filesystem.Directory("sftp://localhost/tmp/")

# create a subdir
dir.make_dir ("data/")

# list contents of the directory
files = dir.list ()

# copy *.dat files into the subdir
for f in files :
    if f ^ '^.*\.dat$' :
        dir.copy (f, "sftp://localhost/tmp/data/")
__init__(url, flags=READ, session)[source]

Construct a new directory object

Parameters:
  • url (saga.Url) – Url of the (remote) directory
  • flagsFlags
  • sessionsaga.Session

The specified directory is expected to exist – otherwise a DoesNotExist exception is raised. Also, the URL must point to a directory (not to a file), otherwise a BadParameter exception is raised.

Example:

# open some directory
dir = saga.filesystem.Directory("sftp://localhost/tmp/")

# and list its contents
files = dir.list ()
open(path, flags=READ)[source]

Open a file in the directory instance namespace. Returns a new file object.

Parameters:
  • path (str()) – The name/path of the file to open
  • flagsFlags
open_dir(path, flags=READ)[source]

Open a directory in the directory instance namespace. Returns a new directory object.

Parameters:
  • path (str()) – The name/path of the directory to open
  • flagsFlags

Example:

# create a subdir 'data' in /tmp
dir = saga.namespace.Directory("sftp://localhost/tmp/")
data = dir.open_dir ('data/', saga.namespace.Create)
get_size(path=None)[source]

Return the size of the directory itself or the entry pointed to by path.

Parameters:path (str()) – (Optional) name/path of an entry

Returns the size of a file or directory (in bytes)

Example:

# inspect a file for its size
dir  = saga.filesystem.Directory("sftp://localhost/tmp/")
size = dir.get_size ('data/data.bin')
print size
close(kill=True, ttype=None)[source]

kill : bool ttype: saga.task.type enum ret: string / bytearray / saga.Task

is_file(path=None)[source]

Returns True if entry points to a file, False otherwise. If path is not none, the entry pointed to by path is inspected instead of the directory object itself.

Parameters:path (str()) – (Optional) name/path of an entry
size

get_size(path=None)

Return the size of the directory itself or the entry pointed to by path.

Parameters:path (str()) – (Optional) name/path of an entry

Returns the size of a file or directory (in bytes)

Example:

# inspect a file for its size
dir  = saga.filesystem.Directory("sftp://localhost/tmp/")
size = dir.get_size ('data/data.bin')
print size