I have a program that takes one file, copies the first column of that file, and then paste the column onto a second file. The program works marvelous, yet I have to assign a variable and equate it to the file path that is typed manually in order to locate the file. I was wondering if by using something like Tkinter to create a dialog box to select and open any file i need, and than store it as my new file path variable, my program would still work fine and it would be better than typing the file path for the variable. However, I attempted to create the dialog box and the two bottoms,yet i don't know how to take the selected files from the dialog box and convert it into my File1 or File 2 variables so the program can do its thing. This is what I've attempted so far:
import os
import csv
import Tkinter
from Tkinter import *
boot = Tkinter.Tk()
topFrame = Frame(root)
topFrame.pack()
Button1 = Button(topFrame, text="Select File 1", fg="red")
Button2 = Button(topFrame, text="Select File 2", fg="blue")
Button1.pack(side=LEFT)
Button2.pack(side=LEFT)
boot.mainloop()
File1 = 'C:/Users/Alan Cedeno/Desktop/Test_Folder/dyn_0.csv'#This is what I need to select from the dialog box
File2 = 'C:/Users/Alan Cedeno/Desktop/Test_Folder/HiSAM1_data_160215_164858.csv'
root, ext = os.path.splitext(File2)
output = root + '-new.csv'
with open(File1) as r1, open(File2) as r2, open(output, 'a') as w:
writer = csv.writer(w)
merge_from = csv.reader(r1)
merge_to = csv.reader(r2)
# skip 3 lines of headers
for _ in range(3):
next(merge_from)
for _ in range(1):
next(merge_to)
for merge_from_row, merge_to_row in zip(merge_from, merge_to):
# insert from col 0 as to col 0
#merge_to_row.insert(0, merge_from_row[0])
# replace from col 1 with to col 3
merge_to_row[0] = merge_from_row[2]
# delete merge_to rows 5,6,7 completely
#del merge_to_row[5:8]
writer.writerow(merge_to_row)
Any help will be appreciate it, I would really love to learn how to do this. Please keep in my I am a slow learner and I am doing this program to interpret data from work and study the atmospheric ozone concentrations of the air here in my city. It seems really, really bad.Please let me know if any formatting must be done in my question. Any input regarding my question will be greatly appreciated. Thanks guys! :)