Python Forum
[Tkinter] logical error - saving file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] logical error - saving file
#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


Messages In This Thread
logical error - saving file - by rwahdan - Jun-30-2021, 09:37 PM
RE: logical error - saving file - by Larz60+ - Jul-01-2021, 12:56 AM
RE: logical error - saving file - by rwahdan - Jul-01-2021, 04:20 AM
RE: logical error - saving file - by Larz60+ - Jul-01-2021, 11:23 AM
RE: logical error - saving file - by rwahdan - Jul-01-2021, 12:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 914 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,346 Apr-01-2023, 12:50 AM
Last Post: woooee
  [PyQt] saving text file by FileDialog option atlass218 14 4,894 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,302 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