简体   繁体   English

Windows计划程序,带有保存文件的Python脚本

[英]Windows scheduler with Python script that saves a file

I currently have a Python script that scrapes some data from the internet, then saves it as a pickle file. 我目前有一个Python脚本,该脚本从Internet上抓取一些数据,然后将其另存为pickle文件。 When running this from a terminal with python filename.py it saves correctly, since the Date Modified field of the pickle file changes. 当从带python filename.py的终端运行此python filename.py时,由于pickle文件的“修改Date Modified字段已更改,因此可以正确保存。 However, when running with the built-in scheduler it doesn't actually save the pickle file, since the Date Modified isn't changed, despite seeing the Python script being executed (terminal opens up and I see the script running). 但是,使用内置调度程序运行时,尽管看到执行了Python脚本(终端打开并且我看到脚本正在运行),但由于“ Date Modified没有更改,因此实际上并没有保存pickle文件。

I ticked the Run with highest privileges box in the scheduler, and despite that it doesn't save the pickle file. 我在调度程序中选中了Run with highest privileges框,尽管它没有保存pickle文件。 I thought it had to do with it not having write permission, but if it has the highest priviliges, surely it can save a file? 我以为它与没有写权限有关,但是如果它具有最高的特权,那么肯定可以保存文件吗?

At the scheduled time a terminal opens, so I know it is actually being executed (print a message to make sure), but it doesn't show an error about the fact that it couldn't save the file or anything like that. 在计划的时间,终端打开,所以我知道它实际上正在执行(打印一条消息以确保),但是它没有显示有关无法保存文件或类似内容的错误。 The only reason I know it's not working is the Date Modified field not changing. 我知道它不起作用的唯一原因是“ Date Modified字段未更改。 How can I fix this? 我怎样才能解决这个问题?

Windows Task Scheduler has a default working directory of C:\\Windows\\System32 . Windows Task Scheduler的默认工作目录为C:\\Windows\\System32 If you set a relative path to the file you are trying to write, it will likely be written into that directory. 如果您设置要尝试写入的文件的相对路径,则可能会将其写入该目录。 If you open a Command Prompt in the directory of your script and run it, the relative path will be that directory. 如果在脚本目录中打开命令提示符并运行它,则相对路径将是该目录。 So, you actually have two copies of the pickle file. 因此,您实际上有两个pickle文件的副本。

If you set an absolute path in your script to the file you want to write to, both methods of running your script will write to the same file. 如果您在脚本中为要写入的文件设置了绝对路径,则两种运行脚本的方法都将写入同一文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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