2. File examples

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)

2.1. Read a certain amount of data from a certain offset in a file

  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:


2.2. Write a chunk of data to a file

  data = 'spam\n'
  f.write(data, offset=15)
  print f.read()

Produces the following output:


2.3. Iterate over a file, delimited by newline characters

  for line in f:
    print '%r' % line

Produces the following output:


2.4. Read all lines from a file into a list

  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()):

['eggs\n', 'and\n', 'spam\n']

2.5. Iterate over a file in chunks of the specified size

  for chunk in f.readchunks(offset=0, chunksize=10):
    print '%r' % chunk

Produces the following output:


2.6. Read scattered data chunks in one operation

  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>