Open text file programmatically via xp_cmdshell & python os.system - python

I'm writing an application that requires a text file to automatically pop open (visible to the user client) after a sequence of database processes are completed. At the end of a SQL file, I have an xp_cmdshell command that reads:
exec xp_cmdshell 'C:\mattermark_etl_project\powershell "C:\mattermark_etl_project\open_file.ps1"'
which executes a powershell script that contains the following:
C:\mattermark_etl_project\python.exe "C:\mattermark_etl_project\open_file.py"
The powershell script executes a python script which contains the following:
import os
def openFile():
fileName = C:\mattermark_etl_project\company_data.txt(ACTIVE)
os.system("C:\mattermark_etl_project\notepad.exe" + fileName)
openFile()
Can someone please help me understand why the text file isn't popping open? SSMS returns output stating the commands ran successfully with "null" results.

Related

Python closes after running program with writing to file

Python keeps closing after I try and run a script. It runs fine and works perfect in my IDE, but when I run the .py from file explorer, it immediately closes, and doesn't write to the file as directed.
I want the program to delete all text on the file, then write 'ToWrite' 4 times.
my_file = open(r"C:\Users\shady\Desktop\copy\python\myfile.txt", "w")
my_file.write("")
my_file.close()
time = [1,1,1,1]
ToWrite = "hello\n"
for x in time:
my_file = open("myfile.txt", "a")
my_file.write(ToWrite)
my_file.close()
I want the program to delete all text on the file, then write 'ToWrite' 4 times.
I tried removing these lines of code from my larger project, and it ran with no isues.
I presume you are running this via the command prompt, and so should be able to see the error message. Please post that also. The main problem is that you are not escaping the backslash.
Try this:
my_file = open("C:\\Users\\shady\\Desktop\\copy\\python\\myfile.txt", "w")
or this:
my_file = open("C:/Users/shady/Desktop/copy/python/myfile.txt", "w")
or this:
import os
filepath = os.sep.join(["C:", "Users", "shady", "Desktop", "copy", "python", "myfile.txt"])
my_file = open(filepath,"w")
If you are not familiar with the command prompt. Open the Windows Start menu and type cmd then select the cmd prompt.
Inside here use cd to change the directory to where your python program is located, then run
py yourprogram.py

controlling stdin and stdout for an EXE fine from within python

I have a file.exe, written by someone else, so I do not have the source code, nor do I have possibility to rewrite it. What the file.exe does is to open the ms-dos prompt, waits for 9 parameter to be provided as input via keyboard and then print text in the prompt (or in a text file if I want using file.exe > text.txt). I want to provide the imput parameter (stdin) from within a python script and read the stdout saving the output to a variable in python. Any chance to do it? I have done something simular with this instruction: os.system('wine tp52win9x.exe < inputfile.txt > res.txt') giving and writing from and to a txt file.

checking for a string from shell output

I am trying to check for information on Linux server,if it has certain disks named 'ocr'.
So I have run a shell command and capture the output in a text file.
Then I will search for the string in the file.But this below script doest work.
import os
myCmd = os.popen('ls /dev/asm|grep ocr').read()
print(myCmd)
with open('myCmd') as f:
if 'ocr' in f.read():
print("RAC server")
and capture the output in a text file
You've saved it in a string variable, then you're trying to open and read a file named myCmd. These likely have different content because it's not clear where you've actually written any files
You don't need a file for the logic of that code
if 'ocr' in myCmd:
print("RAC server")
Also, you really shouldn't be using shell commands if you don't have to
for f in os.listdir("/dev/asm"):
if "ocr" in f:
print("RAC server")
break

Opening .exe from .txt error

I'm currently creating a script that will simply open a program in the SAME directory as the script. I want to have a text file named "target.txt", and basically the script will read what's in "target.txt" and open a file based on its contents.
For example.. The text file will read "program.exe" inside, and the script will read that and open program.exe. The reason I'm doing this is to easily change the program the script opens without having to actually change whats inside.
The current script Im using for this is:
import subprocess
def openclient():
with open("target.txt", "rb") as f:
subprocess.call(f.read())
print '''Your file is opening'''
Its giving me an error saying it cannot find target.txt, even though I have it in the same directory. I have tried taking away the .txt, still nothing. This code actually worked before, however; it stopped working for some strange reason. I'm using PythonWin compiler instead of IDLE, I don't know if this is the reason.
There are two possible issues:
target.txt probably ends with a newline, which messes up subprocess.call()
If target.txt is not in the current directory, you can access the directory containing the currently executing Python file by parsing the magic variable __file__.
However, __file__ is set at script load time, and if the current directory is changed between loading the script and calling openclient(), the value of __file__ may be relative to the old current directory. So you have to save __file__ as an absolute path when the script is first read in, then use it later to access files in the same directory as the script.
This code works for me, with target.txt containing the string date to run the Unix date command:
#!/usr/bin/env python2.7
import os
import subprocess
def openclient(orig__file__=os.path.abspath(__file__)):
target = os.path.join(os.path.dirname(orig__file__), 'target.txt')
with open(target, "rb") as f:
subprocess.call(f.read().strip())
print '''Your file is opening'''
if __name__ == '__main__':
os.chdir('foo')
openclient()

Python not outputing to file when script is run in vb program

I am making a vb text editor that can run scripting code. I have successfully gotten it to run the code and show up but I would like to be able to have the output from the the code redirected to a file without having to do anything to the python code. Is that possible?
This is the code I'm using to try it but it does not write anything to the file:
Shell(compiler & " """ & fileName & " "" > C:\output.txt")
the compiler is the location of python.exe in the python install folder and the file name is the file I'm running.
The problem could also be in the way I am trying to do it with the shell command.
After some more research into the the matter I found a way to make it work.
Here is the working code:
Shell("cmd.exe /c " & compiler & " """ & fileName & " "" > ""C:\output.txt"" ", vbNormalFocus)
So I am actually running the line through the command prompt which is being called by the shell function. My assumption was that shell function was using the command prompt to execute the string argument but I guess I was wrong. Also, just to note, the output file name is also surrounded by quotes which is different than my original post.

Categories

Resources