forked from Jonikas-Lab/Li-Zhang-Patena-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_input.py
executable file
·43 lines (39 loc) · 1.71 KB
/
read_input.py
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
39
40
41
42
43
#! /usr/bin/env python2.7
"""
Read input using various methods (from raw or file arguments or stdin)
"""
import sys,os
debug = 0
### Read the input:
# try reading the argument; if none given, read from stdin (which is what makes it work
# with pipes (echo ctcgag | script.py) and when called on a selection in vi and such).
def read_input():
# if arguments were given:
try:
arguments = sys.argv[1:]
# check if they're a list of files (i.e. if first arg is a valid filename - good enough)
# if yes, print the outputs one by one, with file names.
# TODO this is an issue with large files, since it tries to read the whole thing into memory.
# TODO maybe I could just return the open file (or a fileinput thing) instead of the list of lines?
if os.path.lexists(arguments[0]):
if debug: print "Processing input as a list of files..."
if debug: print "\t%s"%'\n\t'.join(arguments)
input = ''
for infile in arguments:
INFILE = open(infile,'r')
input += INFILE.read()
# otherwise assume it's straight command-line input:
# join all into one string, separated by newlines
else:
input = '\n'.join(arguments)
# if there are no arguments, assume it's going through a pipe and read stdin
# TODO: if stdin is after a newline, on the command line, it never exits! fix? who cares
except IndexError:
input = sys.stdin.read()
# returns the input as a list of lines instead of a single string
return input.split('\n')
### If called directly, test everything
if __name__ == '__main__':
input = read_input()
# TODO what's '\b'?
print '\b'.join(input)