Advertisement
Morbo

adder

Sep 15th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. from sys import argv
  2. from os.path import exists
  3.  
  4. script, arg1, arg2 = argv
  5.  
  6. def copy(a,b):
  7.     open(b,'w').write(open(a).read())
  8.  
  9. def read(a):
  10.     return int(open(a).readline())
  11.  
  12. def write(a,b):
  13.     open(a,'w').write(b)
  14.    
  15. def add(a,b):
  16.     return a+b
  17.  
  18. file1 = open(arg1)
  19. file2 = open(arg2)
  20. print 'This program\'ll take whatever number you write in a file,'
  21. print 'and add it with a number in another file...atleast I hope.'
  22.  
  23. num1 = int(raw_input('Let\'s get a first number: '))
  24. num2 = int(raw_input('and the second number: '))
  25.  
  26.  
  27. print 'Ok, they were %s and %s' % (num1,num2)
  28. print 'Alright, let\'s open up \'%s\', and write %s to it, then %s to \'%s\'' % (arg1,num1,num2,arg2)
  29. print add(num1,num2)
  30. #turn to string to write to file
  31. f = str(num1)
  32. s = str(num2)
  33.  
  34. write(arg1,f)
  35. write(arg2,s)
  36. #close files
  37. file1.close()
  38. file2.close()
  39.  
  40.  
  41. print 'alright, they\'re written..and closed to make sure they\'re not still in buffer'
  42. print 'so now we\'ll reopen them and print out the result of whatever was in the file'
  43.  
  44. print 'Does the file exist? %s' % exists(arg1)
  45. print 'And the other one? %s' % exists(arg2)
  46.  
  47. result = raw_input('Where do you want the result saved to?: ')
  48. file3 = open(result,'w')
  49. print 'Ok, lets add\'em up'
  50. #read files, turn to string then write to result
  51. new_r = str(add(read(arg1),read(arg2)))
  52. write(result,new_r)
  53.  
  54.  
  55. print 'Alright, we\'ll copy the result to a new file then read it, to see if it all worked.'
  56.  
  57. #write to new file then read answer
  58. new_f = raw_input('New file is called what? ')
  59. file4 = open(new_f,'w')
  60. copy(result,new_f)
  61. print 'And the result is~~~~ %s ' % open(new_f).readline()
  62.  
  63. file1.close()
  64. file2.close()
  65. file3.close()
  66. file4.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement