Permission Denied Error When Running Python Script via SQL Server Agent

Kevin Dumitrascu 0 Reputation points
2024-12-30T09:49:22.55+00:00

Environment:

  • SQL Server Agent Job Type: Operating System (CmdExec)
  • Python Version: Python 3.13.1
  • Script Location: C:\Users\Kevin\Desktop\Scripts\test.py

Issue Description: I’m trying to execute a Python script via SQL Server Agent. The script is very simple and aims to create a text file (print.txt) in the same directory as the script. Below are the details:

Python Script (test.py):

with open("print.txt", "w") as file:
	file.write("Hello, World!\n")

Job Step Command:

"C:\Program Files\Python313\python.exe" C:\Users\Kevin\Desktop\Scripts\test.py

When I run the job, it fails with the following error:

PermissionError: [Errno 13] Permission denied: 'print.txt'

What I’ve Tried:

  1. Verified that the SQLSERVERAGENT service is running under the NT Service\SQLSERVERAGENT account.
  2. Assigned Full Control permissions to the NT Service\SQLSERVERAGENT account for the C:\Users\Kevin\Desktop\Scripts folder.
  3. Tested the script manually via Command Prompt (cmd) and it works without issues.

What additional permissions or configurations are needed to allow the SQL Server Agent to create files via a Python script? Could there be other constraints specific to the SQL Server Agent’s account that I might have missed?

SQL Server Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LiHongMSFT-4306 31,566 Reputation points
    2024-12-31T02:22:34.9766667+00:00

    Hi @Kevin Dumitrascu

    Have you checked if the SQL Server Agent service account has write permission to the target directory.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.