Python Forum

Full Version: Executing python script from SQL Server agent job getting error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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())