Search code examples
pythonwindows-task-scheduler

Python script running from windows scheduled task is storing data in wrong folder


I have a script that scrapes data, it saves it to my local db and it takes screenshot from webpages and stores it in a local folder. Both the db and screenshots folder are located in a folder on my Desktop.

Here are some lines of code from my script.

connection = sqlite3.connect('NW.db')

    if not os.path.exists('nw_' + req[1]):
        os.mkdir('nw_' + req[1])
    # folder = f'nw_{req[1]}\{Month}_{Hire_Start}'
    folder = f'nw_{req[1]}\{Hire_Start}'
    if not os.path.exists(folder):
        os.mkdir(folder)

    folder_w = f'nw_{req[1]}'
    if not os.path.exists(folder_w):
        os.mkdir(folder_w)

ss.full_Screenshot(driver, save_path=r'.', image_name=folder + "NW_" + req[1] + "_" + days3 + "_" + Hire_Start + ".jpg")

Everything is working fine and it is stored in the same folder where I am running my script from. But when I make a windows scheduled task to run this script at a specific time it stores the db file and the screenshots folder in the following location C:\Windows\System32 instead of my folder on the Desktop

I made a batch file and I added this to my scheduled task

"C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe" "C:\Users\Administrator\Desktop\Python_Kishen\NW_offer.py"
pause 

Screenshot windows scheduled task


Solution

  • You shouldn't rely on your "current" or "working" directory in a script run from the system. Specify the full path to where you want your output files.

    You can use os.chdir() to make your preferred destination your current dir, but that's not the best way. Full paths.