Here is a very basic function, all I want that the filename is printed:
from tkinter import filedialog as file_dialog
def SaveFile():
MyFile = file_dialog.asksaveasfilename()
print(MyFile)
If I am running this function, the File Save dialog opens as it should. If I enter a filename for the file I want to save, i.e. Test.txt, and press the save button, I get a message the c:/Path/to/Test.xtx does not exist, which is obvious because I want to create it and expected to get a path as result.
If I select an existing file, I can confirm the overwriting warning dialog and then I get the path and filename.
I tried various parameters such as
MyFile = file_dialog.asksaveasfilename(defaultextension=".txt", title="Save File", filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))
But nothing helps. I really don't understand what is going wrong here because Saving a file usually means that it does not exist.
I am on Python 3.11.4
EDIT:
I discovered that I do have this behavior only if I want to save to "Documents" directory. If I choose any other directory it works.
This issue is present on Windows 10, and Windows 11 due to "Controlled Folder Access".
Press the Windows button and type "controlled folder access" to open the system settings page.
From there you have three options.
Hopefully this should resolve your issue.
List of restricted folders : C:\Users<username>\Documents, C:\Users\Public\Documents, C:\Users<username>\Pictures, C:\Users\Public\Pictures, C:\Users\Public\Videos, C:\Users<username>\Videos, C:\Users<username>\Music, C:\Users\Public\Music, C:\Users<username>\Favorites