这里是Python新手。在
我有一个Python脚本来执行一些geodatabase管理(协调/发布版本、压缩等)。我的脚本中有以下代码行:
createLog = open(str(datetime.date.today()) + ".txt", "w")
在脚本的每个步骤中,我使用以下语句添加到文本文件:
^{pr2}$当我在IDE(PyCharm)中运行脚本时,我得到了所需的结果:一个文本文件,每个步骤都写入到.txt文件中。当我在任务调度器中运行它时,不会创建.txt文件,因此没有日志。据我所知,其他一切都在继续。我可以跟踪对数据所做的编辑。在
我以前在任务调度器中经历过类似的事情,但在过去从来没有解决过这个问题。在
有什么想法吗?在
我认为这是一个工作目录问题。Python的open函数打开当前工作目录中的文件,而不是脚本所在的文件夹中的文件。这是一个常见的误解!(这让我在学习Python时困惑了很久…)
那么什么是工作目录?引用我的好朋友维基百科:
(来源:https://en.wikipedia.org/wiki/Working_directory)
那么这个工作目录是如何选择的呢?在
它是由该进程的父进程选择的!当您从类似bash的shell运行程序时,shell(父进程)会将正在运行的程序(子进程)的工作目录设置为当前所在的目录。(即
cd
所指向的目录。)由于您的IDE是智能且有帮助的,因此它启动Python脚本进程并将工作目录设置为脚本本身所在的位置。任务调度程序的帮助不大。。。我完全不知道它设置的工作目录是什么。但是,如果你搜索你的系统,我相信你会发现日志文件就在某处!在
相关问题 更多 >
编程相关推荐