john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

input multi line stdin raw input readline write to file

# 2013-02-13 johnpfeiffer

import os
import sys


class MultilineInput():
    buffer = ''

    def get_multiline_input( self ):
        print 'Enter as many lines as you would like and use Control + C to quit \n\n'
        try:
            while True:
                line = raw_input()
                self.buffer += line
        except KeyboardInterrupt:
            pass

# MAIN ###############################

# version 1 (control + c)
#test = MultilineInput()
#test.get_multiline_input()
# user_input = test.buffer


# version 2 (control + d)
"""
print 'Enter as many lines as you would like and use Control + D to quit \n\n'
user_input = sys.stdin.readlines()

print '\nYOUR CONTENT:\n%s ' % user_input
"""

# version 3

def multi_input():
    try:
        while True:
            data = raw_input()
            if not data: break
            yield data
    except KeyboardInterrupt:
        return


print 'Enter as many lines as you would like and use enter on a blank line to quit \n\n'
user_input = list( multi_input() )

newValue = ''
if len( user_input ) > 1:
    for line in user_input:
        newValue = newValue + line + '\n'
#        print '%s' % ( line.strip() ) # apparently print handles this differently than write()

else:
    newValue = ''.join( user_input )

print '%s' % newValue



print '\n\n'
for line in user_input:
  print '%s' % ( line.strip() ) # apparently print handles this differently than write()

print 'done'


filename = '/var/lib/ssl/new.cert.crt'
target = open( filename , 'w' )
for line in user_input:
    target.write( line )
target.close()

- - - - - - -
# white space delimited String to written to file as separate lines

def write_space_delimited_string_as_lines( source , filename ):
    target = open( filename , 'w' )
    word_list = source.split()
    target.write( '\n'.join( word_list )  )
    target.close()


output = ''
for line in user_input:
    output = output + line + ' '
write_space_delimited_string_as_lines( output , 'c:\\Users\\John\\Desktop\\deleteme.txt' )

  • « Centos6 3 minimal secure server
  • DynamoDBQuery pom »

Published

Feb 13, 2013

Category

python

~212 words

Tags

  • file 92
  • input 7
  • line 31
  • multi 8
  • python 180
  • raw 3
  • readline 1
  • stdin 2
  • to 63
  • write 10