Home > Net >  Name file using first few words from file
Name file using first few words from file

Time:04-27

def save():
   summary = (text.get('1.0', '15.0')).rstrip()
   data = text.get('1.0', END)
   file = open(r"C:\Notes\" summary ".txt", 'w')
   file.write(data)
   file.close()

Hello! I am trying to create a Notes using tkinter. I want to make a function that works "secretly" (I do not want to use asksaveasfile etc.) So what does it mean "secretly": User click save and see for example only: "File saved", but file must use first few words of a text as name. Intuitive, i see this like code above, but this does not work properly.

P.S. I need this path "C:\Notes\...", can not do like open(summary,'w').

CodePudding user response:

Use

file = open(f"C:/Notes/{summary}.txt", w)

Every Windows API accepts either forward or backward slashes, and this avoids the confusion of the \" you currently have.

Note, however, that users will be surprised by this. How will they remember how the file was saved? And what happens if two notes start with the same few words? Notes from project calls often start with the name of the project every time.

  • Related