├── README.md ├── LICENSE └── shuffle.py /README.md: -------------------------------------------------------------------------------- 1 | # textutil-shuffle-file 2 | Randomly shuffle lines in a text file 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 floydhub 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /shuffle.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, division, print_function, unicode_literals 2 | import argparse 3 | import random 4 | 5 | def str2bool(val): 6 | """ 7 | Helper method to convert string to bool 8 | """ 9 | if val is None: 10 | return False 11 | val = val.lower().strip() 12 | if val in ['true', 't', 'yes', 'y', '1', 'on']: 13 | return True 14 | elif val in ['false', 'f', 'no', 'n', '0', 'off']: 15 | return False 16 | 17 | def main(): 18 | """ 19 | Randomly shuffles the lines in a file 20 | """ 21 | 22 | # Parse command line args 23 | parser = argparse.ArgumentParser(description='Randomly shuffle lines in file') 24 | 25 | parser.add_argument( 26 | '-i', '--input', required=True, 27 | help='Path to input file') 28 | parser.add_argument( 29 | '-header', '--hasheader', required=False, type=str2bool, 30 | default='False', help='File has header row?') 31 | parser.add_argument('-o', '--output', required=True, help='Path to output file') 32 | 33 | args = parser.parse_args() 34 | 35 | # Convert args to dict 36 | vargs = vars(args) 37 | 38 | print("\nArguments:") 39 | for arg in vargs: 40 | print("{}={}".format(arg, getattr(args, arg))) 41 | 42 | # Read the input file 43 | with open(args.input, 'r') as inputfile: 44 | with open(args.output, 'w') as outputfile: 45 | print("\nProcessing input") 46 | 47 | # If has header, write it unprocessed 48 | if args.hasheader: 49 | headers = inputfile.readline() 50 | if headers: 51 | outputfile.write(headers) 52 | 53 | lines = inputfile.readlines() 54 | random.shuffle(lines) 55 | outputfile.writelines(lines) 56 | 57 | print("\nDone. Bye!") 58 | 59 | if __name__ == '__main__': 60 | main() --------------------------------------------------------------------------------