File: gfal2_python_read.py

package info (click to toggle)
gfal2-bindings 1.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 688 kB
  • sloc: cpp: 2,574; python: 1,050; sh: 109; makefile: 92
file content (36 lines) | stat: -rwxr-xr-x 862 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Example : open/read/close a file with gfal 2.0
#

import sys
import os
import gfal2
from string import printable

## main func
if __name__ == '__main__':
	# comment for usage
	if(len(sys.argv) < 2):
		print("\nUsage\t %s [gfal_url] \n"%(sys.argv[0]))
		print("    Example: %s lfn:/grid/dteam/myfile "%(sys.argv[0]))
		print("             %s srm://myserver.com/myhome/myfile \n"%(sys.argv[0]))
		os._exit(1)
		
	ctx = gfal2.creat_context()
	# open the file
	f = ctx.open(sys.argv[1], 'r')
	# read first 10 bytesthe max_size first bytes.
	content = f.read(10)
	# Hex dump
	str = []
	for byte in content:
		print("%2X " % ord(byte)),
		if byte in printable:
			str.append(byte)
		else:
			str.append('.')
	print('\t', ' '.join(str))
	
	# no close needed, done automatically with the destruction of the file handle