0

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! :)

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
user665997
  • 313
  • 1
  • 4
  • 18

1 Answers1

1

From the information that you've given us, I believe that the your best approach right now is to forget about the GUI. You can get right into working with the data quicker if you simply just do something like:

option = input('would you like file 1 or 2? (1/2): ')
myfile = File1 if option == 1 else File2
with open(myfile) as fp:
  # do your work with the file

You will get alot more value from your coding time by working with the data rather than spending many frustrating hours with a GUI.

Edit:

If there are alot of files to work with and you want your user to be able to select from them without having to type out the entire filename, I would display the list of files in the directory and ask the user to select from them with a number. (some code taken from How to list all files of a directory?)

from os import listdir
from os.path import join

# get the list of files in mypath and store in a list
mypath = 'C:/Users/Alan Cedeno/Desktop/Test_Folder/'
onlycsv = [f for f in listdir(mypath) if '.csv' in f]

# print out all the files with it's corresponding index
for i in range(len(onlycsv)):
  print( i, onlycsv[i] )

# prompt the user to select the file
option = input('please select a file by number: ')

# build out the full path of the file and open it
fullpath = join(mypath, onlycsv[option])
with open( fullpath ) as fp:
  # do work with file
Community
  • 1
  • 1
brianSan
  • 545
  • 6
  • 17
  • That's a good way of approaching it, but the problem is that I have so many files that I have to pick from, that typing the file path each time for File1 or File2 is very time consuming... I know is hard.... Any other ideas? Thanks for you input btw! :) – user665997 Mar 11 '16 at 17:06
  • dude!! thanks that works so much better!!!! I've made it work now and I am so happy haha next step is to plot this new file, but I think i can handle it from here. – user665997 Mar 11 '16 at 17:47
  • Glad to hear it. Good luck with your project! – brianSan Mar 11 '16 at 23:01