2

I have a python script that runs a few sql scripts using subprocesses.

branch_path = "C:/devel/branch" #path used in command
command_process = subprocess.Popen(
['sqlcmd', '-E', '-V1', '-S', 'MYPC', '-i', branch_path +"/DatabaseScripts/restore_db_mss.sql"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True
)
command_output = command_process.communicate()[0]
print command_output

I get this error:

Sqlcmd: Error: Error occurred while opening or operating on file C: (Reason: Access is denied).

I tried giving my machine the correct permissions but nothing worked. I tried swtching to the service to admin as suggested by others but that is not working either. Any help would be much appreciated

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
3rdeye7
  • 536
  • 4
  • 25
  • 1
    You need to surround the path with quotes. – pmbAustin Jul 26 '16 at 21:20
  • @pmbAustin I tried using the entire path enclosed in brackets instead of concatenating the branch path, still throw the same error. – 3rdeye7 Jul 26 '16 at 21:28
  • May you try reading the path as raw string? `['sqlcmd', '-E', '-V1', '-S', 'MYPC', '-i', r"C:/devel/branch/DatabaseScripts/restore_db_mss.sql"]` On the other hand. Start cmd [right click] "as admin" Also check if the sql server service has permission to "C:\". Does it work in cmd without python? – user2853437 Jul 26 '16 at 21:33
  • Did you try: http://stackoverflow.com/questions/33305856/how-can-i-use-sqlcmd-with-windows-authentication-e-but-without-elevated-perm ? – user2853437 Jul 26 '16 at 21:41
  • @user2853437 nope doesnt work – 3rdeye7 Jul 26 '16 at 22:05
  • I don't think you understood... the path needs to be quoted. You have this .... branch_path +"/DatabaseScripts/restore_db_mss.sql" .... but what you need is something like this: ... "\"" + branch_path + "/DatabaseScripts/restore_db_mss.sql\"" ... Where \" is an escaped quote character that puts the actual quote in the value being passed. – pmbAustin Jul 26 '16 at 22:41

0 Answers0