This page includes some simple examples of how to use the pyxrootd File object to manipulate files on an xrootd server.
We’ll use the following File object as a basis for the rest of the examples:
from XRootD import client
from XRootD.client.flags import OpenFlags
with client.File() as f:
f.open('root://someserver//tmp/eggs', OpenFlags.UPDATE)
f.write('green\neggs\nand\nham\n')
status, data = f.read() # Reads the whole file
print '%r' % data
status, data = f.read(offset=6, size=4) # Reads "eggs"
print '%r' % data
Produces the following output:
'green\neggs\nand\nham\n'
'eggs'
data = 'spam\n'
f.write(data, offset=15)
print f.read()
Produces the following output:
'green\neggs\nand\nspam\n'
for line in f:
print '%r' % line
Produces the following output:
'green\n'
'eggs\n'
'and\n'
'spam\n'
print '%r' % f.readline()
print f.readlines()
Produces the following output (Note how the first line is not returned with the call to readlines() because we ate it with the first call to readline()):
'green\n'
['eggs\n', 'and\n', 'spam\n']
for chunk in f.readchunks(offset=0, chunksize=10):
print '%r' % chunk
Produces the following output:
'green\neggs'
'\nand\nspam\n'
f.write(r'The XROOTD project aims at giving high performance, scalable '
+' fault tolerant access to data repositories of many kinds')
size = f.stat()[1].size
v = [(0, 40), (40, 40), (80, size - 80)]
status, response = f.vector_read(chunks=v)
for chunk in response.chunks:
print chunk
Produces the following output:
<buffer: 'The XROOTD project aims at giving high p', length: 40, offset: 0>
<buffer: 'erformance, scalable fault tolerant acc', length: 40, offset: 40>
<buffer: 'ess to data repositories of many kinds', length: 38, offset: 80>