3.4. Files and Directories¶
3.4.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 = radical.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.4.2. Flags¶
The following constants are defined as valid flags for file and directory methods:
- radical.saga.filesystem.OVERWRITE¶
- radical.saga.filesystem.RECURSIVE¶
- radical.saga.filesystem.CREATE¶
- radical.saga.filesystem.CREATE_PARENTS¶
- radical.saga.filesystem.LOCK¶
- radical.saga.filesystem.EXCLUSIVE¶
- radical.saga.filesystem.DEREFERENCE¶
3.4.3. File – radical.saga.filesystem.File
¶
- class radical.saga.filesystem.File(url=None, flags=512, session=None, _adaptor=None, _adaptor_state={}, _ttype=None)[source]¶
Bases:
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) filesession –
saga.Session
- Fgs:
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 ())
- close(kill=True, ttype=None)[source]¶
kill : bool ttype: saga.task.type enum ret: string / bytearray / saga.Task
- 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 ())
- property 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.4.4. Directory – radical.saga.filesystem.Directory
¶
- class radical.saga.filesystem.Directory(url=None, flags=512, session=None, _adaptor=None, _adaptor_state={}, _ttype=None)[source]¶
Bases:
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) directoryflags – Flags
session –
saga.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 ()
- close(kill=True, ttype=None)[source]¶
kill : bool ttype: saga.task.type enum ret: string / bytearray / saga.Task
- 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)
- 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
- 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
flags – Flags
- 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
flags – Flags
Example:
# create a subdir 'data' in /tmp dir = saga.namespace.Directory("sftp://localhost/tmp/") data = dir.open_dir ('data/', saga.namespace.Create)
- property 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)