How to have various py scripts save to folder created in gui

Viewed 46

I found this piece of code here on Stack Overflow and I integrated it into my gui. I would ask a question if I could, but I don't have the reps for that.

This is the code I am borrowing.

def selectPath():   
    path_ = askdirectory()
    path.set(path_)

def create_file():  
    print("folder_name: ", folder.get())
    print("path_name: ", path.get())
    dirs = os.path.join(path.get(), folder.get())
    if not os.path.exists(dirs):
        os.makedirs(dirs)
        tkinter.messagebox.showinfo('Tips:','Folder name created successfully!')
    else:
        tkinter.messagebox.showerror('Tips','The folder name exists, please change it')

What I would like to do is reference the path in my py files that are executed in the gui. I make a number of intermediate files. It would be nice if they could save to the new folder. For example, how could I change this to be variable and go to the created folder and instead of the location noted below?

df_new.to_csv(r'C:\Users\XXX\File.txt',sep='\t',index=False)

Can I import a portion of the tkinter gui properties/folder name and path? And then insert that into the py output files? Thanks!

1 Answers
"""
RE: Can I import a portion of the tkinter gui properties/folder name and path?
And then insert that into the py output files?

Sounds like you want to prompt for a directory or use a path already in the program.
"""
# prompt to select a directory
import os
from tkinter.filedialog import askdirectory

directory = askdirectory()  # Now we have a variable with at path
print(f'{directory=}')
# which can be used in
# df_new.to_csv(r'C:\Users\XXX\File.txt',sep='\t',index=False)
# Example
destination = os.path.join(directory, 'File.txt')
print(f'{destination=}')

# or if 'C:\Users\XXX\File.txt' is already present in your GUI
print(os.path.split(r'C:\Users\XXX\File.txt'))
# Gives a tuple [0] is the path [1] is the file
extracted_directory = os.path.split(r'C:\Users\XXX\File.txt')[0]
print(f'{extracted_directory=}')
extracted_destination = os.path.join(extracted_directory, 'File.txt')
print(f'{extracted_destination=}')

Demonstration Output

directory='C:/MyFilesHere'
destination='C:/MyFilesHere\\File.txt'
('C:\\Users\\XXX', 'File.txt')
extracted_directory='C:\\Users\\XXX'
extracted_destination='C:\\Users\\XXX\\File.txt'

An example building on the above information and the comments below. Creates a GUI with a button to save to a specific file.

# Import necessary Library
from tkinter import *
from tkinter import ttk


def store_data(directory):
    with open(directory, 'w') as f_out:
        f_out.write('lots of data')


destination = 'C:/MyFilesHere\\File.txt'
# Create an instance of tkinter window
win = Tk()
# Set the geometry of tkinter window
win.geometry("750x250")
# Create a button which will use lambda to call another function with included
# parameter.
btn = ttk.Button(win, text="Store my data", command=lambda: store_data(
    destination))
btn.pack(pady=10)
win.mainloop()
Related