title: Python How do I write a user provided IP address to a file?
body: <p>so here's the problem. I have started using python for a little while and I have come across a problem. Although it may seem basic at first, it has had me busy for hours on end.
  *I do not receive any syntax errors when running the program, although the program fails to write the new IP to file.</p>
  
  <p>I am making a function for a program, that asks the client for a new IP address as the servers(my IP) is currently not static. As my IP changes quite frequently, i would like to give clients (that are attempting to establish a connection with me) the option to change the IP that they are trying to connect to.</p>
  
  <p>So here is the function:</p>
  
  <pre><code>#CONFIGURE NEW IP
  def IpConfigNew():
     #Creates new window 
     IpConfig = Tk()
     IpConfig.configure(background = 'white')
     IpConfig.title('Configure IP')
     IpConfig.geometry('300x60+260+380')
     IpNew = StringVar()
     Label(IpConfig, font  = ('Helvetica',12), text = 'Please enter the IP address of the   server', bg = 'white').place(x=0,y=4)
     #Creates box for user to type IP in.
     Entry(IpConfig,textvariable=IpNew, bg = "light blue").place(x=4,y=35)
  
   #Store New Ip NOTE that it is nested within IpConfigNew
   def IpStore():
       #Retrieves new IP from text box and stores it in variable 
       GetIpNew = IpNew.get()
       mypath = str('Latest Server')
       #Creates directory to write new IP to.
       if not os.path.isdir(mypath):
           os.makedirs(mypath)
       StoreLatestServer = open('Latest Server\NewIp.txt', 'w')
       #Writes new IP
       StoreLatestServer.write("%s"%(GetIpNew))
       StoreLatestServer.close()
       IpConfig.destroy()#Closes window
  
     #Calls on function IpStore in order to store the new IP    
     Button(IpConfig,text = 'Done', command = IpStore).place(x=150,y=30)
     IpConfig.mainloop()
  
  def PromptIpReconfig():
    confirm = tkMessageBox.askyesno(title = "Configure Server IP", message = "Are you   sure?")
  #Checks to see if the user chose to change IP
  if confirm &gt;0: 
    #In the event that the user said yes go to IpConfigNew
    IpConfigNew()
  else:
     return
  
  #Configure Menu Bar #Sets up Menu Bar for parent Window(app)
  menubar = Menu(app)
  filemenu = Menu(menubar,tearoff = 0)
  # Goes to     PromptIpReconfig (Prompts user to Reconfigure the IP after clicking button)
  filemenu.add_command(label="Configure IP", command = PromptIpReconfig) 
  filemenu.add_command(label="Quit", command=app.destroy)
  menubar.add_cascade(label='Options',menu = filemenu)
  app.config(menu=menubar)#Draws menubar on parent window(app)
  </code></pre>
  
  <p>I am not sure why its not working as I have done this before expect slightly differently. When I attempt to write the new IP to file, nothing gets written to the file. The new directory is created, so I know that the function is working. I contrasted between the program that I made a while back that work and this one. 
  What i found was that if I did this it worked fine:</p>
  
  <pre><code>#CONFIGURE NEW IP
  #Creates new window
  IpConfig = Tk()
  IpConfig.configure(background = 'white')
  IpConfig.title('Configure IP')
  IpConfig.geometry('300x60+260+380')
  IpNew = StringVar()
  Label(IpConfig, font  = ('Helvetica',12), text = 'Please enter the IP address of the   server', bg = 'white').place(x=0,y=4)
  #Creates box for user to type IP in.
  Entry(IpConfig,textvariable=IpNew, bg = "light blue").place(x=4,y=35)
  
   #Store New Ip
   def IpStore():
       #Retrieves new IP from text box and stores it in variable GetIpNew
       GetIpNew = IpNew.get()
       mypath = str('Latest Server')
       #Creates directory to write new IP to.
       if not os.path.isdir(mypath):
           os.makedirs(mypath)
       StoreLatestServer = open('Latest Server\NewIp.txt', 'w')
       #Writes new IP
       StoreLatestServer.write("%s"%(GetIpNew))
       StoreLatestServer.close()
       #Closes window
       IpConfig.destroy()
  
  #Calls on function IpStore in order to store the new IP
  Button(IpConfig,text = 'Done', command = IpStore).place(x=150,y=30)
  IpConfig.mainloop()
  </code></pre>
  
  <p>I found that if I initiate without using the menu bar and instead just initiate it at program start, it works fine. I am not sure whether the problem is calling the IpConfigNew function from the menu bar or whether it has something to do with the fact that I am nesting functions. </p>
  
  <p>I would love it if someone could help me out here as its been bugging me for days!</p>