Python Forum
[Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename
#1
Hello everyone, hope you are all well these holidays.

I'm having problems updating and writing to a config.ini file using filedialog.askopenfilename in Tkinter.

Example of my config file:
[Keyword Files]
kwA = 100_questions.txt
kwB = 91K_nouns.txt
kwC = 31K_verbs.txt
kwD = 28K_adjectives.txt
kwE = 6K_adverbs.txt
Here is the bit of code, it's assigned already to a tkinter button:

    def kw_txt1(self):    
        kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')])
        config = configparser()
        x = re.sub("/", "\\\\", kwTXT1)
        y = re.sub("/", "//", kwTXT1)
        print(x)
        print(y)
        print(kwTXT1)
        config.set('Keyword Files', 'kwA', 'kwTXT1')
        config.write('C:/Users/Stryker/Desktop/py/config.ini')
As you can see, there is a lot there but it's because I've been trying many different solution for this error here.:
Error:
File "C:\Users\Stryker\AppData\Local\Programs\Python\Python38\lib\configparser.py", line 902, in set raise NoSectionError(section) from None configparser.NoSectionError: No section: 'Keyword Files'
I've been reading up everwhere and I got to thinking it's the way the filepath was set, but I tried using different 'experimental ways' to see if I can get it in. I've been able to generate all these filepaths:
C:\Users\Stryker\Desktop\py\31K_verbs.txt
C:/Users/Stryker/Desktop/py/31K_verbs.txt
C://Users//Stryker//Desktop//py//31K_verbs.txt

But I still get the error that the section 'Keyword Files' is missing, when it clearly is there on my config file.

I even tried many variations of .split(os.path.sep).os.path.join(os.path.sep) but nothing. Any ideas?

I can read from config file no problem, I've gotten reading from config file just fine, it's writing the file name into the my config file that has me stumped.

Any help is greatly appreciated. Thanks for reading.
Reply


Messages In This Thread
Unable to save filepath to config.ini file using filedialog.askopenfilename - by JackMack118 - Dec-28-2019, 07:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] [split] I want to add a code to the "Save" button to save the data entered LOO 1 2,017 Jan-23-2023, 05:31 PM
Last Post: deanhystad
  [Tkinter] filedialog, open a file error liketocode 4 3,477 Dec-07-2022, 10:51 PM
Last Post: liketocode
  [Tkinter] Exclude hidden file, filedialog.askopenfile red380sl 1 3,204 Dec-01-2020, 07:04 PM
Last Post: DT2000
  Can you help me to understand the "asksaveasfile" and "askopenfilename" functions? aquerci 4 4,335 May-23-2020, 12:11 AM
Last Post: aquerci
  [Tkinter] Unable to get current contents of the entry field where the content is the file path pmpinaki 1 2,259 Apr-18-2020, 06:45 PM
Last Post: deanhystad
  [PyQt] saving text file by FileDialog option atlass218 14 4,742 Feb-19-2020, 09:22 AM
Last Post: atlass218
  [Tkinter] HOW TO: Set [Label].config() by variable name reference? gazoxtapod 4 5,004 Apr-17-2019, 09:57 PM
Last Post: gazoxtapod
  Save image from Clipboard Linux / save Pixbuf format M_Schneider 1 2,873 May-04-2018, 04:38 PM
Last Post: woooee
  tkinter filedialog and pickle - custom icon question. kim07133 0 2,789 Jan-08-2018, 12:10 PM
Last Post: kim07133
  [Tkinter] filedialog. FULL SCREEN issac_n 0 3,173 Dec-05-2017, 07:33 AM
Last Post: issac_n

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020