In Python, write a program that is able to combine two text files. It should hav
ID: 3572418 • Letter: I
Question
In Python, write a program that is able to combine two text files. It should have a "master" file and an "add" file. The program should open a GUI and let them select their "add" file. Once the file has been selected, the program will add any data in the file to the end of the "master" file, overwriting the previous version (it should not make a new "master file each time). All new entries to the "master" file should also be "datestamped". For example:
The master file contains:
1 10/12/15
2 10/12/15
And the add file contains:
3
4
When the program runs, and the user selectes the add file, the new master file should look like this:
1 10/12/15
2 10/12/15
3 12/4/16
4 12/4/16
Please note that the format must be a plaintext file and information should be displayed like above (each individual entry on its own line)
Explanation / Answer
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
f1=open(filename,"r") #opening user selected file in read mode
text=f1.read() #reading the content of user selected file
f2=open("master.txt","a+") #opening master file in append mode
f2.write(text) #writing user selected file content
f3=open("master.txt","r") #opening master file in read mode
text1=f3.read()
print(text1) #printing contents of master file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.