Files
galaxy/tools/filters/randomlines.py
T
Nicola Soranzo 21b44bf348 Fix all E201 and E202 style errors
using the following command:
```
autopep8 -i -r --exclude $(sed -e 's|^|./|' -e 's|/$||' .ci/flake8_blacklist.txt | paste -sd,) --select E201,E202 .
```
2017-08-17 11:35:39 +01:00

37 lines
840 B
Python

#!/usr/bin/env python
# Kanwei Li, 2010
# Selects N random lines from a file and outputs to another file
import random
import sys
def main():
infile = open(sys.argv[1], 'r')
total_lines = int(sys.argv[2])
if total_lines < 1:
sys.stderr.write("Must select at least one line.")
sys.exit()
kept = []
n = 0
for line in infile:
line = line.rstrip("\n")
n += 1
if (n <= total_lines):
kept.append(line)
elif random.randint(1, n) <= total_lines:
kept.pop(random.randint(0, total_lines - 1))
kept.append(line)
if n < total_lines:
sys.stderr.write("Error: asked to select more lines than there were in the file.")
sys.exit()
open(sys.argv[3], 'w').write("\n".join(kept))
if __name__ == "__main__":
main()