Archive

Posts Tagged ‘.py’

python script: copy contents and replace a word

October 15, 2009 Leave a comment


a script that will copy the contents from one file to another and also replace the requested word with the other one.


_________________________________________________________________________________________________


#!/usr/bin/env python

the_word=raw_input(‘enter the word to replace –> ‘)
replace_word=raw_input(‘enter the word to replace with –> ‘)

to_file=open(‘/testfile.txt2′, ‘a’)

try:
      read_file=open(‘/testfile.txt’)

      for searches in read_file:
              searches=searches.replace(the_word, replace_word)

      to_file.write(searches)
finally:
      to_file.close()


_________________________________________________________________________________________________

(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)

Categories: Python Tags: , , ,

python script: to copy from one file to another

October 15, 2009 2 comments


A simple python script to copy a content of one file to another.


________________________________________________________________________________________________________


#!/usr/bin/env python

to_readfile=open(‘/testfile.txt’, ‘r’)
try:
      reading_file=to_readfile.read()

      writefile=open(‘/testfile2.txt’, ‘w’)
      try:
           writefile.write(reading_file)
      finally:
           writefile.close()

finally:
      to_readfile.close()




________________________________________________________________________________________________________


(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)

Follow

Get every new post delivered to your Inbox.