# Buggy code
def checkForOpenFile(self, c, fn):
    """Warn if fn is already open and add fn to already_open_files list."""
    d, tag = g.app.db, "open-leo-files"
    if g.app.reverting:
        # Fix #302: revert to saved doesn't reset external file change monitoring
        g.app.already_open_files = []
    if (
        d is None
        or g.app.unitTesting
        or g.app.batchMode
        or g.app.reverting
        or g.app.inBridge
    ):
        return
    aList = g.app.db.get(tag) or []
    if [x for x in aList if os.path.samefile(x, fn)]:
        # The file may be open in another copy of Leo, or not:
        # another Leo may have been killed prematurely.
        # Put the file on the global list.
        # A dialog will warn the user such files later.
        if fn not in g.app.already_open_files:
            g.es("may be open in another Leo:", color="red")
            g.es(fn)
            g.app.already_open_files.append(fn)
    else:
        g.app.rememberOpenFile(fn)


# Traceback (FileNotFoundError)
(base) c:\leo.repo\leo-editor>python c:\leo.repo\leo-editor\launchLeo.py --gui=qttabs --gui=qttabs leo\core\leoPy.leo leo\plugins\leoPlugins.leo

Leo 6.2-b1-devel, devel branch, build 5da736ea4d
2020-02-29 06:19:36 -0600
Unexpected exception reading 'c:/leo.repo/leo-editor/leo/core/leoPy.leo'
Traceback (most recent call last):

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 2575, in doPostPluginsInit
c = lm.loadLocalFile(fn, gui=g.app.gui, old_c=None)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3297, in loadLocalFile
c = lm.openFileByName(fn, gui, old_c, previousSettings)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3335, in openFileByName
ok = lm.readOpenedLeoFile(c, fn, readAtFileNodesFlag, theFile)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3505, in readOpenedLeoFile
readAtFileNodesFlag=readAtFileNodesFlag)

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 695, in openLeoFile
silent=silent,

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 553, in getLeoFile
g.app.checkForOpenFile(c, fileName)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in checkForOpenFile
if [x for x in aList if os.path.samefile(x, fn)]:

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in <listcomp>
if [x for x in aList if os.path.samefile(x, fn)]:

File "C:\Users\edreamleo\Anaconda3\lib\genericpath.py", line 96, in samefile
s1 = os.stat(f1)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Users/edreamleo/Desktop/leo_unittest_position.leo'

Can not create empty workbook
Traceback (most recent call last):

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 2602, in doPostPluginsInit
c1 = lm.openEmptyWorkBook()

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 2645, in openEmptyWorkBook
c = lm.loadLocalFile(fn, gui=g.app.gui, old_c=None)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3297, in loadLocalFile
c = lm.openFileByName(fn, gui, old_c, previousSettings)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3335, in openFileByName
ok = lm.readOpenedLeoFile(c, fn, readAtFileNodesFlag, theFile)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3505, in readOpenedLeoFile
readAtFileNodesFlag=readAtFileNodesFlag)

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 695, in openLeoFile
silent=silent,

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 553, in getLeoFile
g.app.checkForOpenFile(c, fileName)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in checkForOpenFile
if [x for x in aList if os.path.samefile(x, fn)]:

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in <listcomp>
if [x for x in aList if os.path.samefile(x, fn)]:

File "C:\Users\edreamleo\Anaconda3\lib\genericpath.py", line 96, in samefile
s1 = os.stat(f1)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Users/edreamleo/Desktop/leo_unittest_position.leo'

# Fixed code
def checkForOpenFile(self, c, fn):
    """Warn if fn is already open and add fn to already_open_files list."""
    d, tag = g.app.db, "open-leo-files"
    if g.app.reverting:
        # #302: revert to saved doesn't reset external file change monitoring
        g.app.already_open_files = []
    if (
        d is None
        or g.app.unitTesting
        or g.app.batchMode
        or g.app.reverting
        or g.app.inBridge
    ):
        return
    # #1519: check os.path.exists.
    aList = g.app.db.get(tag) or []
    if [x for x in aList if os.path.exists(x) and os.path.samefile(x, fn)]:
        # The file may be open in another copy of Leo, or not:
        # another Leo may have been killed prematurely.
        # Put the file on the global list.
        # A dialog will warn the user such files later.
        if fn not in g.app.already_open_files:
            g.es("may be open in another Leo:", color="red")
            g.es(fn)
            g.app.already_open_files.append(fn)
    else:
        g.app.rememberOpenFile(fn)