2. XRootD.client.File
: File-based operations¶
- class XRootD.client.File[source]¶
Interact with an
xrootd
server to perform file-based operations such as reading, writing, vector reading, etc.
2.1. Similarities with Python built-in file object¶
To provide an interface like the python built-in file object, the __iter__(), next(), readline() and readlines() methods have been implemented. These look for newlines in files, which may not always be appropriate, especially for binary data.
Additionally, these methods can’t be called asynchronously, and they don’t
return an XRootDStatus
object like the others. You only get the data that
was read.
2.2. Class Reference¶
2.2.1. Methods¶
- File.open(url, flags=0, mode=0, timeout=0, callback=None)[source]¶
Open the file pointed to by the given URL.
- Parameters:
url (string) – url of the file to be opened
flags – An ORed combination of
XRootD.client.flags.OpenFlags
where the default is OpenFlags.NONEmode – access mode for new files, an ORed combination of
XRootD.client.flags.AccessMode
where the default is AccessMode.NONE
- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object and None
- File.close(timeout=0, callback=None)[source]¶
Close the file.
- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object and None
As of Python 2.5, you can avoid having to call this method explicitly if you use the with statement. For example, the following code will automatically close f when the with block is exited:
from __future__ import with_statement # This isn't required in Python 2.6 with client.File() as f: f.open("root://someserver//somefile") for line in f: print line,
- File.stat(force=False, timeout=0, callback=None)[source]¶
Obtain status information for this file.
- Parameters:
force (boolean) – do not use the cached information, force re-stating
- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object andXRootD.client.responses.StatInfo
object
- File.read(offset=0, size=0, timeout=0, callback=None)[source]¶
Read a data chunk from a given offset.
- Parameters:
offset (integer) – offset from the beginning of the file
size (integer) – number of bytes to be read
- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object and the data that was read
- File.readline(offset=0, size=0, chunksize=0)[source]¶
Read a data chunk from a given offset, until the first newline or EOF encountered.
- Parameters:
offset (integer) – offset from the beginning of the file
size (integer) – maximum number of bytes to be read
chunksize (integer) – size of chunk used for reading, in bytes
- Returns:
data that was read, including the trailing newline
- Return type:
string
- File.readlines(offset=0, size=0, chunksize=0)[source]¶
Read lines from a given offset until EOF encountered. Return list of lines read.
- Parameters:
offset (integer) – offset from the beginning of the file
size (integer) – maximum number of bytes to be read
chunksize (integer) – size of chunk used for reading, in bytes
- Returns:
data that was read, including trailing newlines
- Return type:
list of strings
Warning
This method will read the whole file into memory if you don’t specify an offset. Think twice about using it if your files are big.
- File.readchunks(offset=0, chunksize=2097152)[source]¶
Return an iterator object which will read data chunks from a given offset of the given chunksize until EOF.
- Parameters:
offset (integer) – offset from the beginning of the file
chunksize (integer) – size of chunk to read, in bytes
- Returns:
iterator object
- File.write(buffer, offset=0, size=0, timeout=0, callback=None)[source]¶
Write a data chunk at a given offset.
- Parameters:
buffer – data to be written
offset (integer) – offset from the beginning of the file
size (integer) – number of bytes to be written
- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object and None
- File.sync(timeout=0, callback=None)[source]¶
Commit all pending disk writes.
- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object and None
- File.truncate(size, timeout=0, callback=None)[source]¶
Truncate the file to a particular size.
- Parameters:
size (integer) – desired size of the file
- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object and None
- File.vector_read(chunks, timeout=0, callback=None)[source]¶
Read scattered data chunks in one operation.
- Parameters:
chunks (list of 2-tuples of the form (offset, size)) – list of the chunks to be read. The default maximum chunk size is 2097136 bytes and the default maximum number of chunks per request is 1024. The server may be queried using
XRootD.client.FileSystem.query()
for the actual settings.- Returns:
tuple containing
XRootD.client.responses.XRootDStatus
object andXRootD.client.responses.VectorReadInfo
object