-1

I am making a file editor with python idle. I have a part where if the file requested doesn't exist it inserts "No such file exists" in the text box (textA). However instead of giving the error message a file is created. My code is:

def addtofile():
    name = entryA.get()
    try:
        file = open(name, "a")
        content = entryB.get()
        file.write(content)
        file.close()
        windowa.destroy()
        start()
    except:
        content = "No such file exists"
        textA.delete(0.0, END)
        textA.insert(END, content)

I open the file using 'a' so the previous content doesn't get erased. I think the issue is with how I am opening the file. Can you tell me how to open (I know 'a', 'r', 'w' and 'w+') the file where it doesn't overwrite previous content but doesn't create a file if one doesn't exist?

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Thomas
  • 1,214
  • 4
  • 18
  • 45
  • What is the point in *not* creating a file if it doesn’t exist yet? – mkrieger1 Mar 22 '20 at 14:42
  • IDK @mkrieger1 - I just already have a function for that somewhere else. – Thomas Mar 22 '20 at 14:46
  • Sort of. I just didn't know that that was a way of doing it. ┐(´•_•`)┌ @mkrieger1 – Thomas Mar 22 '20 at 14:50
  • You code used 'entryB', which is not defined. Should that be 'entryA' or is your code incomplete? 'windowa' is undefined, and unneeded for your question. See https://stackoverflow.com/help/minimal-reproducible-example about writing code to be posted. – Terry Jan Reedy Mar 23 '20 at 20:50

1 Answers1

1

Try something like:

from os.path import isfile

def addtofile():
    name = entryA.get()
    try:
        if isfile(name):
            file = open(name, "a")
            content = entryB.get()
            file.write(content)
            file.close()
            windowa.destroy()
            start()
         else:
             print("no file in path exists")
    except:
        content = "No such file exists"
        textA.delete(0.0, END)
        textA.insert(END, content)

This is assuming the name returns a file name and the path to where the file needs to be saved. Else the file will be save where the code is called.

A better option would be look into the fact that the path to the directory of the file as well as the file exists or not.

Something of the likes:

import os
from os.path import isfile

def open_file(path_to_directory):
    """Return file descriptor for file"""
    if os.path.exists(path_to_directory):
        if isfile(path_to_directory):
            print('File in path Found')
            with open(path_to_directory) as c_file:
                return c_file
        else:
            print('File not found.')
            raise FileNotFoundError(path_to_directory)
    else:
        print('Path to Directory of File not found.')
        raise NotADirectoryError(path_to_directory)

In your code use the function as

file = open_file(name)
# .. Operations
file.write()
file.close()
Shan-Desai
  • 3,101
  • 3
  • 46
  • 89
  • The first one works fine. I just had to remove the try except because it still wasn't inserting the error message but i have fixed that. Thank's for your help :) – Thomas Mar 22 '20 at 14:44