简体   繁体   English

Python计划任务未写入文件

[英]Python Scheduled task does not write to file

I have created a Scheduled Task on Windows 7 to run a Python Script which will write a string to a file. 我已经在Windows 7上创建了计划任务来运行Python脚本,该脚本会将字符串写入文件。

To test this, I created the following Python script: 为了对此进行测试,我创建了以下Python脚本:

#! /usr/bin/python

f = open("output.txt", "w")

f.write("hello")
f.close()

The script was saved in the directory: C:\\Users\\NeonFlash\\Desktop\\files\\ with the filename: test.py 脚本保存在目录:C:\\ Users \\ NeonFlash \\ Desktop \\ files \\中,文件名:test.py

The Scheduled Task was configured as: 计划任务配置为:

Program to Run: "C:\Python27\python.exe"
Program Arguments: "C:\Users\NeonFlash\Desktop\files\test.py"

When the scheduled task runs, it does not create the file. 计划的任务运行时,它不会创建文件。

I even selected the option "Run with highest privileges" for the scheduled task. 我什至为计划任务选择了“以最高特权运行”选项。

Below is the output of net user command: 下面是net user命令的输出:

User accounts for \\NeonFlash-PC

-------------------------------------------------------------------------------
Administrator            Guest                    NeonFlash
The command completed successfully.

When the script is run directly from command prompt, it successfully creates the output file. 直接从命令提示符运行脚本时,它将成功创建输出文件。 The issue occurs only when it is launched using a Scheduled Task. 仅当使用计划任务启动该问题时,才会发生此问题。

I believe the problem is the working directory of your python is different when it is run by the task scheduler, the output file is in another folder you just have to find it ...just did a search, the default is the system32 folder, find your output.txt there. 我相信问题是当由任务计划程序运行时,您的python的工作目录是不同的,输出文件位于另一个文件夹中,您只需要查找它即可...只是进行了搜索,默认为system32文件夹,在此处找到您的output.txt。

this is what you want: 这就是你想要的:

#! /usr/bin/python

f = open("C:\\Users\\NeonFlash\\Desktop\\files\\output.txt", "w")

f.write("hello")
f.close()

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

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