Python Forum
[Tkinter] logical error - saving file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] logical error - saving file
#1
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.
Reply
#2
Please explain what you are trying to accomplish here.
I expect that there is a simple answer.
Reply
#3
(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.
Reply
#4
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")
rwahdan likes this post
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 541 Mar-12-2024, 07:45 PM
Last Post: ejKDE
  Why is the program not running? Is there a logical or syntax problem? behi00 10 2,153 Apr-01-2023, 12:50 AM
Last Post: woooee
  [PyQt] saving text file by FileDialog option atlass218 14 4,680 Feb-19-2020, 09:22 AM
Last Post: atlass218
  Part of code is adding extra new line when saving to text file. lovepeace 9 5,038 Aug-24-2019, 12:52 PM
Last Post: lovepeace

Forum Jump:

User Panel Messages

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