Python Forum
path.exists returning True when it shouldn't
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
path.exists returning True when it shouldn't
#1
Python version used is 3.5.3
Python file is executed on OS Debian 9.11

This script checks if files in 'source folder' are already in 'destination folder'.
If not, move it to 'destination folder'.
Else, move it to 'duplicate folder'.

#!/usr/bin/env python3

import os
import os.path
from os import path

SRC_PATH = "/mnt/fsdata/IT/Servers/PostGres/CDR"
DEST_PATH = SRC_PATH + "/procd"
DUP_PATH = SRC_PATH + "/dup"

# r=root, d=directories, f = files
for r, d, f in os.walk(SRC_PATH):
        for file in f:
                filePathName = r + "/" + file
                # check if file already exists in /procd folder:
                if path.exists(DEST_PATH + "/" + file):
                    print('file exists')
                    os.rename(filePathName, DUP_PATH + "/" + file)
                    continue

                # move file
                os.rename(filePathName, DEST_PATH + "/" + file)

        break
Files in SRC_PATH are physicaly on a Windows machine.

When i execute the script once, path.exists returns False.
When i execute the script a second time, path.exists returns True.

When I copy and paste the files from DEST_PATH to SRC_PATH and delete them from DEST_PATH and execute the script, path.exists returns False.

However, when i cut the files from DEST_PATH and paste it back to SRC_PATH (and DEST_PATH is thus empty), path.exists still returns True. Why?
Same when I "drag and drop" from DEST_PATH to SRC_PATH.

Someone has an idea about this mystery?

thx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,215 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  pathlib destpath.exists() true even file does not exist NaN 9 4,693 Dec-01-2020, 12:43 PM
Last Post: NaN
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 2,166 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Returning True or False vs. True or None trevorkavanaugh 6 9,263 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  os.path.isdir(<whatever>) returns always True Fabrizio 3 4,697 Dec-04-2018, 04:28 PM
Last Post: nilamo
  Returning true or false in a for loop bbop1232012 3 8,154 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  path.exists() problem CAHinton 2 2,904 Jul-24-2018, 05:47 PM
Last Post: CAHinton
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,775 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908
  os.path.exists apparently doesn't always work! Larz60+ 2 4,866 Oct-10-2017, 10:16 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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