How to pass the output of one .py script which I executed in GDB using below command:
(gdb)source myfilename.py
to another .py script myfilename2.py
E.g: On running myfile myfilename.py in GDB I get the below results
(gdb)source myfilename.py
(gdb)print $function("input")
(gdb)$1 = someoutput`
If I want to pass $1 as input to another .py script how do I do it.
Thanks for the help.
I'm not sure if you can run other python programs output to another in gdb, but of course you can pass output of a command like this.
(gdb) run "`python -c 'print "\xff\xff\xff\xff"'`"
May be you can try passing the second .py file instead of the command for its output to be passed into first .py.
You cannot use contents of GDB variable $1 for your program input, since program launch is performed by shell, not by gdb.
You can store your script output in file, launch gdb for your executable and use command
(gdb)run <outputfile
Or use a named pipe, as suggested in this answer.
Related
I'm attempting to run a Linux script through Python's subprocess module. Below is the subprocess command:
result = subprocess.run(['/dir/scripts/0_texts.sh'], shell=True)
print(result)
Here is the 0_texts.sh script file:
cd /dir/TXTs
pylanguagetool text_0.txt > comments_0.txt
The subprocess command executes the script file, writing a new comments_0.txt in the correct directory. However, there's an error in the execution. The comments_0.txt contains an error of "input file is required", and the subprocess result returns returncode=2. When I run the pylanguagetool text_0.txt > comments_0.txt directly in the terminal the command executes properly, with the comments_0.txt written with the proper input file of text_0.txt.
Any suggestions on what I'm missing?
There is some ambiguity here in that it's not obvious which shell is run each time 0_texts.sh is invoked, and whether it has the values you expect of environment variables like PATH, which could result in a different copy of pylanguagetool running from when you call it at the command line.
First I'd suggest removing the shell=True option in subprocess.run, which is only involving another, potentially different shell here. Next I would change subprocess.run(['/dir/scripts/0_texts.sh']) to subprocess.run(['bash', '/dir/scripts/0_texts.sh']) (or whichever shell you wanted to run, probably bash or dash) to remove that source of ambiguity. Finally, you can try using type pylanguagetool in the script, invoking pylanguagetool with its full path, or calling bash /dir/scripts/0_texts.sh from your terminal to debug the situation further.
A bigger-picture issue is, pyLanguageTool is a Python library, so you're almost certainly going to be better off calling its functions from your original Python script directly instead of using a shell script as an intermediary.
I am a noob, self-motivated programmer, and had been researching methods to use my Python script to run a Powershell file that will copy and image and place the image into Excel.
I've used the subprocess, call, and Popen commands in an attempt to call and run the Powershell program from the Python script, but none has worked.
Some of the examples I found only called different functions of a Powershell script, but when I tried those settings it didn't work for my program. All of the setup for my Powershell has been established so that it can run with my PC, and also runs well when launched independently from Python.
What I would like to ask is if I had, for example, a My_program.py file and a Moving_image.ps1 file. I want to use my .py file to run/execute my .ps1 file, while both programs are located in the same path (C:\Users\Scripts).
What line of code(s), imports, and other program setup's would I need in my Python file to simply run the independent .ps1 file from my Python script?
I don't need the Powershell script to return anything to the Python script. I would like for it to simply run the copy and paste the command I sent it.
Thank you. Any type of guidance that will lead to this program actually functioning properly will be most appreciated!
Here's what worked for me (testing on linux):
python script test.py
from subprocess import call
call(["/opt/microsoft/powershell/6.0.4/pwsh", "./test.ps1"])
powershell script test.ps1
Import-Module '/home/veefu/pwshmodules/testMod'
Write-Output "Hello from test.ps1"
Get-HelloWorld
testMod.psm1 module, stored at /home/veefu/pwshmodules/testMod
function Get-HelloWorld {
Write-Output "Hello World (from Module)"
}
result when running the python script:
Hello from test.ps1
Hello World (from Module)
On windows you'll probably have to provide the complete path, C:\Windows\system32\MicrosoftPowerShell\1.0\powershell.exe and you may have to pass /file .\yourOtherScript.ps1 in the second argument to call
Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.
I wanted to write a bat file in which the following are the commands
python
print "hello"
I wanted it to work like it will open python command utility and execute
print "hello"
If the above two commands are written in bat file
python command utility is getting opened and waiting after I terminate it manually,The next command is getting executed
Is there any way by which I can redirect the above print "hello" command into python command utility
I know we can write a separate python file with print "hello" and call that directly by
python filename.py
But my requirement is specifically the redirection which I have mentioned above
Any help in this regard is greatly appreciated
Thanks in advance!!!
I believe you will need to use the python -c command, followed by the code you wish to execute.
The code will need to be properly enclosed in quotes or double quotes (I believe it depends on whether you wish to use multi-line commands).
Try python -c print "hello"
From the docs:
-c <command>
Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.
If this option is given, the first element of sys.argv will be "-c" and the current directory will be added to the start of sys.path (allowing modules in that directory to be imported as top level modules).
https://docs.python.org/2/using/cmdline.html
Why don't you create a temp pyfile dynamically from the BAT ?
#echo off
set $Command=print "hello"
echo %$command% >temp.py
python temp.py
del temp.py
echo next
pause & exit /b
Is it feasible to run a program in Python's subprocess module, but with files from Terminal?
So I want to run the following program from within Python:
myProgram -a myArg
However, suppose that the above program requires the file myFile in the current directory, and it doesn't take the required file as an argument. So, if you run the above program in the directory where there is myFile, the program succeeds in processing. However, if you run it in the directory where there is NOT myFile, the execution fails.
And when I tried to execute the program from within Python's subprocess.Popen(), with shell=True, the program doesn't work and it looks like the reason it failed is the program wasn't able to read myFile when executed from within Python.
So, is there any way to run it successfully from within Python?
subprocess.Popen('myProgram -a myArg', cwd='/folder/with_myFile/')
Similar Question: Python specify popen working directory via argument