Python Forum
Executing python script from SQL Server agent job getting error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Executing python script from SQL Server agent job getting error
#1
I am new to python. I am using Python 3 and I am trying to execute a PYTHON script that is only combining 2 csv files via a .bat file in SQL Server 2016 Agent job but getting an error. ALL files, the .bat, .csv files and the .py files all sit in the SAME share directory. Any help/direction would be appreciated. Thanks.

Here is my what is in my .bat file:

python \\abicfs2\shared\dev\DATAEXPORT\MonthlyReports\combineCSVFiles.py

Here is the code in the combineCSVFiles.py file:

import os

filename0 = os.path.join(os.path.dirname("BoundPoliciesAddedAtFaultEndorsementWithin10Days_Combined.csv"))
filename1 = os.path.join(os.path.dirname("BoundPoliciesAddedAtFaultEndorsementWithin10Days_Counts.csv"))
filename2 = os.path.join(os.path.dirname("BoundPoliciesAddedAtFaultEndorsementWithin10Days_Details.csv"))
f = open(filename0, "w+")
f.close()
fin1 = open(filename1, "r")
data1 = fin1.read()
fin1.close()
fin2 = open(filename2, "r")
data2 = fin2.read()
fin2.close()
fout = open(filename0, "a")
fout.write(data1)
fout.write(data2)
fout.close()
Here is the error I'm getting in the SQL Server agent job:

Quote:Message Executed as user: domain\sqlsvcAA. C:\Windows\system32>python \abicfs2\shared\dev\DATAEXPORT\MonthlyReports\combineCSVFiles.py
Traceback (most recent call last): File "\abicfs2\shared\dev\DATAEXPORT\MonthlyReports\combineCSVFiles.py", line 9, in f = open(filename0, "w+") FileNotFoundError: [Errno 2] No such file or directory: ''. Process Exit Code 1. The step failed.
Reply
#2
error line number doesn't match code. what's changed?
what is the purpose of lines 6 & 7?
on line 14, if the file doesn't exist, it will be created.
also , you should be using with to open files, your code can be re-writtes as (**Note** Not Tested):
import os
 
filename0 = os.path.join(os.path.dirname("BoundPoliciesAddedAtFaultEndorsementWithin10Days_Combined.csv"))
filename1 = os.path.join(os.path.dirname("BoundPoliciesAddedAtFaultEndorsementWithin10Days_Counts.csv"))
filename2 = os.path.join(os.path.dirname("BoundPoliciesAddedAtFaultEndorsementWithin10Days_Details.csv"))

with open(filename1) as fin1, open(filename2) as fin2, open(filename0, 'a') as fout:
    fout.write(fin1.read())
    fout.write(fin2.read())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 387 Jan-26-2024, 07:02 PM
Last Post: deanhystad
  Syntax error while executing the Python code in Linux DivAsh 8 1,655 Jul-19-2023, 06:27 PM
Last Post: Lahearle
Sad "PriceSystem" Python Script error to Shopify API Alphetto 0 451 Jul-04-2023, 10:03 AM
Last Post: Alphetto
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,329 Jun-29-2023, 11:57 AM
Last Post: gologica
  Error 1064 (42000) when executing UPDATE SQL gratiszzzz 7 1,515 May-22-2023, 02:38 PM
Last Post: buran
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,235 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Server Folder Error : WinError5 Access Denied fioranosnake 1 1,147 Jun-21-2022, 11:11 PM
Last Post: Larz60+
Question How can I import a variable from another script without executing it ThomasFab 12 7,875 May-06-2022, 03:21 PM
Last Post: bowlofred
  No module named '_cffi_backend' error with executable not with python script stephanh 2 5,707 Nov-25-2021, 06:52 AM
Last Post: stephanh
  Real-Time output of server script on a client script. throwaway34 2 2,076 Oct-03-2021, 09:37 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020