-1

I want the input, someone has put into the Entry widget (E1) of my Tkinter GUI, to be the name of the new folder, because every time someone inputs something, I need to have a new folder named after the input:

def create():
    folder = E1.get()
    newpath = r"C:\Users\....\folder" 
    if not os.path.exists(newpath):
        os.makedirs(newpath) 

This creates a new folder, but it is named folder and not how I want it named (after the numbers entered into the Entry box (E1)).

Making it:

newpath = r"C:\Users\...\E1.get()" 

gives me a folder called "E1.get()"

And secondly, but that hopefully comes with the answer to the first question, how do I get to see the input, without putting E1.get() into a variable? So is there a way to see it directly and maybe use that as the name of my new folder?

martineau
  • 119,623
  • 25
  • 170
  • 301
hccavs19
  • 165
  • 3
  • 9
  • 1
    Use `newpath = os.path.join(r"C:\Users\Heinrich\Documents\Python\hope\", E1.get()) ` – eyllanesc Jan 18 '18 at 00:09
  • Running it now, gives me a NameError in the "if-not" statement : name 'os' is not defined. I imported join from os.path so from os.path import join – hccavs19 Jan 18 '18 at 01:22
  • import os ...... – eyllanesc Jan 18 '18 at 01:43
  • _'how do I get to see the input, without putting `E1.get()` into a variable?'_ What do you mean? Can't you visually see what's in the entry? or do you mean like `print(E1.get())`? `E1` is an entry widget and `E1.get()` is simply the `str` representation of what's written in that entry. – Nae Jan 18 '18 at 12:05
  • I got it now, thanks a lot everyone ! – hccavs19 Jan 18 '18 at 19:57

1 Answers1

1

There are several ways to do this:

  • String formatting old style:

    newpath = r"C:\Users\Heinrich\Documents\Python\hope\%s" % E1.get()
    
  • String formatting new style:

    newpath = r"C:\Users\Heinrich\Documents\Python\hope\{}".format(E1.get())
    
  • Raw f(ormat)-strings (Python 3.6 only):

    newpath = fr"C:\Users\Heinrich\Documents\Python\hope\{E1.get()}"
    
  • Use os.path.join as @eyllanesc stated:

    from os.path import join
    newpath = join(r"C:\Users\Heinrich\Documents\Python\hope", '1234'))
    
Christian Dean
  • 22,138
  • 7
  • 54
  • 87