This may be a very simple question, but I'm new to python and haven't been able to figure it out.
I want to do something very simple: call a subprocess and send to it two variables at the start (eg: G=[0,1] and K=3), that it needs to run. From the subprocess I want to receive back a list of values in my original script.
I manage to get it to run without any problems:
os.system('abaqus cae noGUI=E11_1')
...but the sending and receiving values I can not figure out. Can anyone give me a suggestion?
I solved my problem.
For anyone seeing this and also working on abaqus like me. To get interact with an abaqus script you need to use the "abaqus python" module (instead of "abaqus CAE"). By coding like this:
os.system('abaqus python script.py var1 var2')
you will send var1 and var2 (as string) to script.py. There you can access the variables with
v1 = int(sys.argv[1]) (= var1)
v2 = int(sys.argv[2]) (= var2)
In the python.py script you can access an .odb with:
odb = openOdb(path+odbname+'.odb')
and do whichever operations you want.
I hope this helps anyone who has the same problem.
Related
I won't lie I know this is a very easily answered question, I'm just having trouble with the documentation and given how specific I feel this is it is a little hard to find solutions online.
I'm running Raspbian on a Raspberry Pi and are attempting to write a python script that operates with the signal strength of a Bluetooth connection. In the Linux command line, if I input hcitool rssi 14:C8:9B:92:C2:05 it will return RSSI return value: 12. What I'm trying to do is store this value in a python variable in the script.
In my script I have tried both importing os, and importing subprocess modules based of the documentation I have seen online, and have tried these solutions:
var = os.system('hcitool rssi 14:C8:9B:92:C2:05')
def find_dist():
return(os.system('hcitool rssi 14:C8:9B:92:C2:05'))
var=find_dist()
var = subprocess.call('hcitool rssi 14:C8:9B:92:C2:05', shell = true)
The os module will simply output the regular RSSI return value: 12 and print 0 for all variable values, and the subprocess module I can't really figure out the proper syntax to. I know this may seem simple, but given the specific nature I'm having trouble finding the proper documentation~
Any help would be greatly appreciated!
The easiest way to do that is with subprocess.run which will capture the output so you can look at it later. Here's the most simple example to get a feel for how it works:
import subprocess
result = subprocess.run(['echo', 'hi'], capture_output=True)
print(result.stdout)
Which will print the expected hi as the output.
I hope the title makes sense. To give specifics:
I am using csvtotable (https://github.com/vividvilla/csvtotable) to generate HTML tables from CSVs. I have installed via pip and am able to run a command line command:
csvtotable test1743.csv test1743.html
to generate a HTML page. All good so far.
I wanted to do this from within a Python script I had already written so I heard that subprocess was the way to do this. I looked up how to do it and understood that it can be done using the following:
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
So I tested this via the command line first by doing
python
from the command line and then running
import subprocess
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
Success! It worked. Fantastic.
However, when I try to do this from IDLE, it just returns a 1. I have checked the directory thinking that maybe the csv was missing from there, but it still doesn't work.
Am I misunderstanding how subprocess works?
Solved by finding a way to call the function without subprocess. I think the issue may have related to default arguments not being set when it is executed through python and hence why below I have had to specify so many arguments.
Code:
from csvtotable import convert
content = convert.convert("C:\\Users\\admin\\Google Drive\\test1743.csv",delimiter=",",quotechar='"',display_length=-1,overwrite=False,serve=False,pagination=True,virtual_scroll=1000, no_header=False, export=True, export_options=["copy","csv","json","print"])
convert.save("C:\\Users\\admin\\Google Drive\\test1743.html",content)
Note that the argument names had to be changed where they had a - in the name. I just changed any instance e.g. display-length to display_length in convert.py
I would like to know how can i use my variables in output of another command. For example if i try to generate some keys with "openssl" i'll get the question about the country, state, organizations etc.
I would like to use my variables in the script that i have to fill this information. I'll have variable "Country"; variable "State" etc. and to be parsed/set in to this questions from the openssl command when is executed.
I'm trying this in bash but also would like to know how will be the same think done in python.
Kind regards
You have multiple ways to do so.
1. If you have your script launched before the python script and the result set in an enviroment variable you can read the environment variable from your python script as follows:
import os
os.environ.get('MYVARIABLE', 'Default val')
Otherwise you can try to launch the other application from your python script and read the result by using os.popen():
import os
tmp = os.popen("ls").read()
or better (if you have a python newer than 2.6)
import subprocess
proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
tmp = proc.stdout.read()
I did hours on research regarding the following question but I wasn't able to find an answer at all. Though there seem to be many fellows having problems with that. I hope I will recieve some help from the community. ;)
I have a Cshell script where I need to call a Python3 script from. Also I am passing a variable.
.csh
#!/bin/csh -f
set variable = value
/../geos.py $variable
So far so fine. In my Python3 script I take this variable, do some calculations and now want to pass back the 'new_variable' to the VERY SAME C shell script in order to proceed my set of data.
.py
import os
...
new_variable = 'foobar'
os.environ['new_variable'] = new_variable
return new_variable
My actual goal is that my C Shell script:
#!/bin/csh -f
set variable = value
/../geos.py $variable
echo $new_variable
doesn't return 'Undefined variable'. So obviously my code doesn't work. Sure, I might be able to temporarily save the python calculations into a file but this seems quite unconvincingly. Also, I understand that it is just not possible to manipulate an environmental variable of the shell through a child process, but still I only want to pass a normal variable. There should be one way, no?
If it is possible, I wasn't able to figure out any solution using subprocess.check_call. What am I missing?
E D I T:
Merci beaucoup.
I knew that there must have been an easy solution. Thanks a lot!
For CSHELL the following code worked:
set new_variable=`../geos.py $variable`
echo $new_variable
For BASH the following code worked:
new_variable=`../geos.py $variable`
echo $new_variable
In the python script itself you don't need to do anything but putting your desired variable into standard output, e.g. print(you_even_can_name_them_as_you_want). No os.environ oo whatever necessary. Made my day. SOLVED
in bash I'd use:
new_variable=$(../geos.py $variable)
Have the python script produce the new value as standard out (i.e. print(new_variable) )
In csh I don't know, maybe you would have to use backquotes instead of $() ?
I've got a bash file that I normally execute using Cygwin.
I need to run this file from my Python code.
I tried this:
for bashfile in files:
p = Popen(bashfile, cwd=dname) #dname is the current directory of the script
stdout, stderr = p.communicate()
I've also seen a similar question here, but when trying to run it that way it says that it can't find the directory of my bash file...
Any ideas? Thanks! :-)
Edit: bashfile has a full path.
Do you need its output to get it directly to Python? If not this may be very fast and easy solution:
os.system("""here some code you use to execute in Terminal""")
You can also try this, though it does (and will no matter what you try) matter where the directory is. This, as far as the output goes, may be a little bit cleaner than the os method.
import commands
cmd="bash ./script.sh"
commands.getoutput(cmd)
If the case is that you need to change the directory:
cmd = "/path/to/your/script/script.sh"
The added benefit of using this method, versus say, os is that you can assign the output to a variable...
fun_times = commands.getoutput("bash ./script.sh")
whereas...
not_fun_times = os.system("./script.sh")
will throw an error.
etc, etc.