从Windows计划的任务运行的Python脚本是将数据存储在错误的文件夹中
我有一个可以刮擦数据的脚本,它将其保存到我的本地数据库中,并从网页中屏幕截图并将其存储在本地文件夹中。 DB和屏幕截图文件夹都位于我桌面上的文件夹中。
这是我脚本中的一些代码行。
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")
一切正常,并且存储在我运行脚本的同一文件夹中。但是,当我在特定时间内完成一个窗口计划的任务来运行此脚本时,它将存储DB文件和屏幕截图文件夹在以下位置 c:\ windows \ system32 而不是我的文件夹在台式机上
I制作了一个批处理文件,然后将其添加到计划的任务
"C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe" "C:\Users\Administrator\Desktop\Python_Kishen\NW_offer.py"
pause
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该在系统运行的脚本中依靠您的“当前”或“工作”目录。指定要输出文件的整个路径。
您 can 使用os.chdir()使您的首选目的地成为当前的dir,但这不是最好的方法。完整的路径。
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.
调度程序进程可能具有不同的位置,并且看起来您正在使用相对路径。如果您有一个想要的地方,我建议使用完整的路径。然后,代码从何处运行都没关系。
作为旁注,看起来您正在做很多重复的弦构建。为什么不这样做并将结果存储在变量中?只需在需要该值的几个地方使用变量即可。
The scheduler process likely has a different location it operates from, and it looks like you're using relative paths. I'd recommend using full paths if you have a place you want this all to go. Then it doesn't matter where the code is run from.
As a side note, it looks like you're doing a lot of repeat string building. Why not do that once and store the result in a variable? Just use the variable in the few places you need that value.