简体   繁体   中英

Windows scheduled task python read file when computer locked

I am very new to Python 3. I have written a script and used pyinstaller to convert it to an EXE. The script attempts to read from a text file named file.txt in the same directory as the EXE. This works just fine when running the EXE from task scheduler while the workstation is logged in and not locked. However, if I try to run the EXE from task scheduler with the user logged in and the workstation locked, I get the error:

[Errno 2] No such file or directory: 'file.txt'. 

This is my python simplified down to the current error (literally just put this in a new python file and ran it the same way and get the same error):

with open('file.txt', 'rb') as myfile:
    readfile = myfile.read()
print(readfile)

I have tried to search for this problem, but all I can find is how to lock/unlock a workstation with python or how to lock/unlock a file with python.

I am fairly certain this isn't a python problem and may just be an intended effect of using task scheduler when the station is locked but I am hoping there is a setting I can change to allow access to files when the workstation is locked or something else.

The file may not be accessible by the script for the following reasons related to how the task is configured:

  1. The task is not configured to run without the user being logged on. Fix: Task properties -> General -> Security options, make sure "Run whether user is logged on or not" is selected. Also make sure that the right user is running the task.
  2. The task may be starting in the wrong directory. Fix: Task properties -> Actions -> Double-click your action -> "Start in (optional)", put in the correct starting directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM