PYTHON   87

file concat py

Guest on 21st August 2022 08:38:38 AM

  1. import sys
  2.  
  3. '''
  4. Concatenates all arguments together, assuming they are files, into the last argument file.
  5. '''
  6.  
  7. if len(sys.argv) < 4:
  8.         print "Usage: file_concat.py <inputfile1>, <inputfile2>, ...  <outputfile>"
  9.         exit()
  10. else:
  11.         ins = sys.argv[1:-1]
  12.         out = sys.argv[-1]
  13.         outfile = open(out, 'w')
  14.  
  15.         all_lines = []
  16.         for i in ins:
  17.                 f = open(i, 'r')
  18.                 lines = [x.strip() for x in f.readlines()]
  19.                 all_lines += lines
  20.  
  21.         outfile.write('\n'.join(all_lines))

Raw Paste


Login or Register to edit or fork this paste. It's free.