[Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename (/thread-23403.html) Pages:
1
2
|
Unable to save filepath to config.ini file using filedialog.askopenfilename - JackMack118 - Dec-28-2019 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.txtHere 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.: 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. RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - Larz60+ - Dec-28-2019 look into pathlib (It's the new (python 3.6) Object-oriented way to deal with filesystem paths): https://docs.python.org/3/library/pathlib.html RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - JackMack118 - Dec-28-2019 (Dec-28-2019, 01:30 PM)Larz60+ Wrote: look into pathlib (It's the new (python 3.6) Object-oriented way to deal with filesystem paths): https://docs.python.org/3/library/pathlib.htmlCan you give me an example of how to make this work? This has me a little overwhelmed. RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - JackMack118 - Dec-28-2019 Grrrrr, working with filepaths is HARD! OK, I'm closer, but no cigar: def kw_txt1(self): kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')]) config = ConfigParser() path = pathlib.Path(kwTXT1) print(kwTXT1) print(path) config.set('Keyword Files', 'kwA', 'path') config.write('config.ini')And I get the same error. And as it can be seen I am able to create an acceptable path: I think I need to figure out how to put the 'r,' before the variable 'kwTXT1', not sure.Any help appreciated, this filepath stuff is harder than I've come across yet. RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - JackMack118 - Dec-28-2019 I'm trying to debug this in steps, but I just can't get it: def kw_txt1(self): kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')]) config = ConfigParser() print(kwTXT1) aaa = 'C:/Users/Stryker/Desktop/py/6K_adverbs.txt' print(aaa) path = pathlib.Path(aaa) print(path) print(aaa) config.set('Keyword Files', 'kwA', 'aaa') config.write('config.ini') -----------------def kw_txt1(self): kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')]) config = ConfigParser() print(kwTXT1) aaa = 'C:/Users/Stryker/Desktop/py/6K_adverbs.txt' print(aaa) path = pathlib.Path(aaa) print(path) print(aaa) config.set('Keyword Files', 'kwA', 'path') config.write('config.ini')
Sorry to keep answering my own thread, but I'm showing the different things I'm trying to no avail. This here I tried to sort of hack something (anything!) in and converting to string: def kw_txt1(self): kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')]) config = ConfigParser() print(kwTXT1) aaa = '6K_adverbs.txt' print(aaa) path = pathlib.PureWindowsPath(r'C:\Users\gahjelle\realpython\file.txt') print(path) print(aaa) config.set('Keyword Files', 'kwA', str(path)) config.write('config.ini')------------- def kw_txt1(self): kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')]) config = ConfigParser() print(kwTXT1) aaa = '6K_adverbs.txt' print(aaa) path = pathlib.Path(r'C:\Users\gahjelle\realpython\file.txt') print(path) print(aaa) config.set('Keyword Files', 'kwA', str(path)) config.write('config.ini')And the same error, when my config has the section on it:
RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - Larz60+ - Dec-28-2019 Here's an example from one of my applications, Note that I wanted all of my paths to be relative so the the application could be run from any location: This is a large implementation, so you should be able to see that advantage and interrelationship of project paths # Relative pathlib path list (base path is Scraper/src) # Includes common files. # # Author: Larz60+ (c) 2018 from pathlib import Path import os import sys class ScraperPaths: def __init__(self): # set starting path = src directory os.chdir(os.path.abspath(os.path.dirname(__file__))) self.homepath = self.get_root_dir(rootnode='src') self.rootpath = self.homepath / '..' self.basepath = self.homepath / 'BaseFiles' # self.virtualbin = self.rootpath / 'venv' / 'bin' self.geckopath = Path('/usr/local/bin') / 'geckodriver' self.chromepath = Path('/usr/local/bin') / 'chromedriver' self.document_path = self.rootpath / 'doc' self.document_path.mkdir(exist_ok=True) self.datapath = self.rootpath / 'data' self.datapath.mkdir(exist_ok=True) self.RawDatapath = self.datapath / 'RawData' self.RawDatapath.mkdir(exist_ok=True) self.codespath = self.RawDatapath / 'Codes' self.codespath.mkdir(exist_ok=True) self.datalogpath = self.datapath / 'logs' self.datalogpath.mkdir(exist_ok=True) self.jsonpath = self.scrapedatapath / 'json' self.jsonpath.mkdir(exist_ok=True) self.htmlpath = self.scrapedatapath / 'html' self.htmlpath.mkdir(exist_ok=True) self.tmppath = self.datapath / 'tmp' self.tmppath.mkdir(exist_ok=True) def display_dict(self, thedict): for key, value in thedict.items(): if isinstance(value, dict): print(f'{key}:') self.display_dict(value) else: print(f' {key}: {value}') if __name__ == '__main__': ScraperPaths()There's also code in there to create organized dictionary list, not used here, but might prove useful for other purposes. examples: run to list all files in json directory import ScraperPaths spath = ScraperPaths.ScraperPaths() p = spath.jsonpath print([x for x in p.iterdir() if x.is_file()]) RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - JackMack118 - Dec-28-2019 Wow thanks @Larz60+ I have a lot to look through here, will get back ... thanks! RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - JackMack118 - Dec-29-2019 Ok, after looking through your help there, I thought I could figure it out but I'm a little confused. Can I just use a part for .txt files? That's all I need, and how could I implement it? Sorry for being a bug, and thanks for your help. RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - Larz60+ - Dec-29-2019 you should read the documentation here: https://docs.python.org/3/library/pathlib.html It's well written, and you'll get a lot out of it Here's a few things that you can do once you have a path: assume: from pathlib import Path import os class MyPaths: def __init__(self): # set starting path = script path os.chdir(os.path.abspath(os.path.dirname(__file__))) self.homepath = Path('.') # Make a data path in script directory self.datapath = self.homepath / 'data' self.datapath.mkdir(exist_ok=True) # put a text file in datapath my_text_file = self.datapath / 'MyTextFile.txt' with my_text_file.open('w') as fp: fp.write(f"Just any old text") # Ok now show fill path of text file: print(f"\nMyTextFile.txt path is: {my_text_file.resolve()}") # Show just the filename print(f"\nfilename: {my_text_file.name}") # Show the stem of filename (part before .) print(f"\nfilename: {my_text_file.stem}") # Show file suffix: print(f"\nfilename: {my_text_file.suffix}") # Get directory list of just files in data: print(f"\nAll files: {[filename for filename in self.datapath.iterdir() if filename.is_file()]}") # create a sub directory in data, and then list just directories newpath = self.datapath / 'newpath' newpath.mkdir(exist_ok=True) print(f"\nAll dirs: {[filename for filename in self.datapath.iterdir() if filename.is_dir()]}") if __name__ == '__main__': MyPaths()results: Additional reading: https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/https://pbpython.com/pathlib-intro.html RE: Unable to save filepath to config.ini file using filedialog.askopenfilename - JackMack118 - Dec-29-2019 I think ny problem is something different now, to be honest. I've been doing some debugging not using a filepath and I still get the same problem: On 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 [hereitgoes] hereitmustbe = change meThe bit of code: def kw_txt1(self): kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')]) config = ConfigParser() print(kwTXT1) aaa = 'this' bbb = str(aaa) print(aaa) print(bbb) config.set('hereitgoes', 'hereitmustbe', 'bbb') config.write('C:/Users/Stryker/Desktop/py/config.ini')The error: If I can't figure this out, which I really want to do, I may fall back and use a .csv file to store settings for my tkinter app, I don't want to though … :\Also tried without full path to config, which is in the folder of all my .py stuff: def kw_txt1(self): kwTXT1 = filedialog.askopenfilename(filetypes =[('Text File', '*.txt')]) config = ConfigParser() print(kwTXT1) aaa = 'this' bbb = str(aaa) print(aaa) print(bbb) config.set('hereitgoes', 'hereitmustbe', 'bbb') config.write('config.ini') |