Python Forum
[Tkinter] logical error - saving file - 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] logical error - saving file (/thread-34145.html)



logical error - saving file - rwahdan - Jun-30-2021

I am trying to open new file but want to check if there is something in the current file before opening a new file so it can be saved. I have the following if statements.

global open_status_name
open_status_name = False

def new_file():
    global open_status_name
    #first statement checking if there is file name and the textbox is not empty, this will Triger save function
    if open_status_name and len(my_text.get(1.0,END)) != 0:
        save_file()
    #second statement checking if there is no file name and the textbox is not empty, this will Triger save as function
    elif (open_status_name == False) and (len(my_text.get(1.0,END)) != 0):
        save_as_file()
    #last statement checking if there is no file name and the textbox is empty, this will Triger new file function
    elif (open_status_name == False) and (len(my_text.get(1.0,END)) == 0):
        return
    my_text.delete("1.0", END)
    root.title('New File')
    status_bar.config(text="New File        ")

    open_status_name = False
open_status_name
is false as it doesn't hold a file name (the file is new at the start of the program but it will change while the program is running). for the if statements i am checking if there is a file name and if the file is empty or not. When I am opening new file and the current file is empty it is going for save as but it should go for a new file without saving as there is nothing there.

Thanks.


RE: logical error - saving file - Larz60+ - Jul-01-2021

Please explain what you are trying to accomplish here.
I expect that there is a simple answer.


RE: logical error - saving file - rwahdan - Jul-01-2021

(Jul-01-2021, 12:56 AM)Larz60+ Wrote: Please explain what you are trying to accomplish here.
I expect that there is a simple answer.

I am creating text editor and while the new file is open the user wants to open a newer file. I want to check if the text widget is empty or not so I can either save the file or not. The simple question is how to check if the text widget is empty.


RE: logical error - saving file - Larz60+ - Jul-01-2021

There is a simpler way to check.
If you use pathlib to open files, you can use:
from pathlib import Path
import os

# make sure in proper directory (this sets dir same as script)
os.chdir(os.path.abspath(os.path.dirname(__file__)))

# example file
filename = Path("myfile.txt")

if filename.stat().st_size:
   print("file has size")
else:
    print("file is empty")
if not using pathlib, use instead:
import os

# make sure in proper directory (this sets dir same as script)
os.chdir(os.path.abspath(os.path.dirname(__file__)))

# example file
filename = "myfile.txt"

if os.stat("myfile.txt").st_size:
    print("file has size")
else:
    print("file is empty")



RE: logical error - saving file - rwahdan - Jul-01-2021

(Jul-01-2021, 11:23 AM)Larz60+ Wrote: There is a simpler way to check.
If you use pathlib to open files, you can use:
from pathlib import Path
import os

# make sure in proper directory (this sets dir same as script)
os.chdir(os.path.abspath(os.path.dirname(__file__)))

# example file
filename = Path("myfile.txt")

if filename.stat().st_size:
   print("file has size")
else:
    print("file is empty")
if not using pathlib, use instead:
import os

# make sure in proper directory (this sets dir same as script)
os.chdir(os.path.abspath(os.path.dirname(__file__)))

# example file
filename = "myfile.txt"

if os.stat("myfile.txt").st_size:
    print("file has size")
else:
    print("file is empty")

Thanks for the big help