-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
38 lines (29 loc) · 869 Bytes
/
utility.py
File metadata and controls
38 lines (29 loc) · 869 Bytes
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
37
38
#!/usr/bin/env python
'''
Description: Utility functions to read file.
Author: Siddharth Sinha (sid3345@gmail.com)
'''
import sys
import gzip
# define reading file exception
class ErrorReadingFile(Exception):
"Raised when Error reading file"
def read_file(input_file):
'''
Input: Input file
Output: IO Handler
'''
if input_file is None:
io_handler = sys.stdin
elif input_file.endswith('.gz'): # Zip file
try:
# This reads the file directly without unzipping it
io_handler=gzip.open(input_file)
except ErrorReadingFile:
sys.exit("Error reading gzipped file: " + input_file)
else:
try:
io_handler = open(input_file, encoding="utf-8")
except ErrorReadingFile:
sys.exit("Error reading file: " + input_file)
return io_handler