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'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/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
  os.path.exists fails with accented characters glenndrives 11 3,046 Sep-03-2024, 05:30 PM
Last Post: glenndrives
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 3,272 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  pathlib destpath.exists() true even file does not exist NaN 9 7,159 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,786 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Returning True or False vs. True or None trevorkavanaugh 6 15,392 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  os.path.isdir(<whatever>) returns always True Fabrizio 3 6,266 Dec-04-2018, 04:28 PM
Last Post: nilamo
  Returning true or false in a for loop bbop1232012 3 11,358 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  path.exists() problem CAHinton 2 3,725 Jul-24-2018, 05:47 PM
Last Post: CAHinton
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 7,726 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908
  os.path.exists apparently doesn't always work! Larz60+ 2 5,757 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