Python Forum
path.exists() problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: path.exists() problem (/thread-11735.html)



path.exists() problem - CAHinton - Jul-23-2018

I can't get Python to recognize a folder as existing from a batch file or command line. However it works fine within the debugger. Thanks in advance for help.

here's an example of the batch file failing. The folder is not a symbolic link or on a network drive.
Error:
C:\proj\python\Handle_Final_Test_Logs_V_0.0.3_23_July_2018>Handle_FTLogs_TestProcessor C:\proj\python\Handle_Final_Test_Logs_V_0.0.3_23_July_2018>python "HFT_V_0.0.3.p y" -s "C:\proj\python\Handle_Final_Test_Logs\Test_Data\testSource" -d "C:\proj \python\Handle_Final_Test_Logs\Test_Data\testDest" Source C:\proj\python\Handle_Final_Test_Logs\Test_Data\testSource is not a folde r
here's the output from the script executing within the debugger:
Quote:process C:\proj\python\Handle_Final_Test_Logs_V_0.0.3_23_July_2018\Test_Data\testSource
23_07_2018_16:45:03 create folder C:\proj\python\Handle_Final_Test_Logs_V_0.0.3_23_July_2018\Test_Data\testDest\H180455
23_07_2018_16:45:03 move file H180455_2018_0718_165519_event_log.sorted.csv to folder H180455
23_07_2018_16:45:03 move file H180455_2018_0718_165519_periodic_log.sorted.csv to folder H180455
23_07_2018_16:45:03 create folder C:\proj\python\Handle_Final_Test_Logs_V_0.0.3_23_July_2018\Test_Data\testDest\H180456
23_07_2018_16:45:03 move file H180456_2018_0718_162618_event_log.sorted.csv to folder H180456
23_07_2018_16:45:03 move file H180456_2018_0718_162618_periodic_log.sorted.csv to folder H180456
check H180455
H180455_2018_0718_165519_event_log.sorted.csv
H180455_2018_0718_165519_periodic_log.sorted.csv
check H180456
H180456_2018_0718_162618_event_log.sorted.csv
H180456_2018_0718_162618_periodic_log.sorted.csv

here's the relevant portions of the script:
import os, sys, shutil, datetime, time, argparse, keyboard
from pathlib import Path
# globals
folders = []    # list of new folders
sourceFolder = ''
destFolder = ''

parser = argparse.ArgumentParser(description = 'Handle Final Test Logs')
parser.prog = 'Handle Final Test Logs, V0.0.2'
parser.add_argument('-s', '--src', help="source folder, default current")
parser.add_argument('-d', '--dest', help="dest folder, default current")
parser.add_argument('-w', '--watch', default = False, help="watch source, default process source")
args = parser.parse_args()
# other stuff omitted

# check source folder.
sourceFolder = args.src
if (sourceFolder == None): 
    sourceFolder = os.getcwd()
if os.path.exists(sourceFolder):
    if os.path.isdir(sourceFolder):
        pass
    else:
        print ('Source %s is not a folder' % sourceFolder)
        sys.exit(1)
else:
    print ('Source %s does not exist' % sourceFolder)
    sys.exit(1)
finally, here's the relevant portion of my json file:

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "stopOnEntry": true,
            "args": [
                "-sC:\\proj\\python\\Handle_Final_Test_Logs_V_0.0.3_23_July_2018\\Test_Data\\testSource",
                "-dC:\\proj\\python\\Handle_Final_Test_Logs_V_0.0.3_23_July_2018\\Test_Data\\testDest",

            ]
        },



RE: path.exists() problem - woooee - Jul-24-2018

Use a forward slash in Python. A "\t" is translated as a tab, not as part of a directory string.


RE: path.exists() problem - CAHinton - Jul-24-2018

Thanks! I belatedly forgot to menton I was using 3.7 in vscode on Windows 7.

I resolved the issue by adding a space between the option and string in the command line arguments. :

works: python "HFT_V_0.0.3.py" -s "C:\proj\python\HFT_0.0.3\Test_Data\testSource"

doesn't: python "HFT_V_0.0.3.py" -s"C:\proj\python\HFT_0.0.3\Test_Data\testSource"