I really tried to find an answer to my problem but couldn't find one, so I hope I didn't double post.
I am running a raspberry pi using debian and python27. After boot up I run a script which determines some general variables I will need for other scripts. I need this variables in different scripts because they are running during different times using crontab.
Normally after the boot up the script is done and I have all my necessary variables in a text file.
I can access from all different other scripts to this text file.
But know I try to avoid to corrupt my SD card of my raspberry and I want to use the read-only mode.
Is there an opportunity to use variables which are created/changed by a python script without using a text file?
I could also run a script with an infinite loop, but I try to avoid this.
Any other suggestions? Is it possible to use environmental variables?
Thanks for every answer!
Max
Edit1:
Thanks for the suggestion the answer from adrianX, but I tried this small example and it doesn't work:
script1:
import os
os.environ["variable1"] = "value1"
After executing the script1, I run the second script.
script2:
import os
print os.environ.get["variable1"]
But this doesn't work? Maybe my question wasn't clear enough?
A non python specific solution would be: http://www.domoticz.com/wiki/Setting_up_a_RAM_drive_on_Raspberry_Pi and writing the file to the /var/tmp the file will disappear on reboot of course.
Related
First of all, I am new to coding, and I have looked for similar questions and I know some commands which can be used in Python and in CMD to open a file using an executable. The problem I'm having is that when I try to use those commands, the program runs without any error, but it doesn't give the output file that should give. On the other hand, when I just double click the file, which is set to open with *.exe, it works and produces the output.
I tried the CMD command:
start "path of .exe" "filepath"
also just:
"path of .exe" "filepath"
Then I tried the os and subprocess modules in python:
subprocess.Popen(...)
os.system(...)
subprocess.run(...)
and many other solution I found on the internet. The point is that all of these solution don't give errors and should work, but they don't produce the wanted file for me. I used the same commands on another file with a different .exe and they work.
This is the step that is not working in my attempt to automate a whole process. If someone is willing to take a look at the files, you can download them from the following link:
https://gofile.io/?c=5TJtS9
The files are as follows:
1. running the rdam.grd file with hist_dam2d.exe produces the hist.plotps file
2. then running the hist.plotps with plotps.exe produces the wanted diagram
It should be an easy task that doesn't work for me.
For more information... This is part a random finite element software which is freely distributed by the authors. You can see the whole documentation and download all parts of the program from this link:
http://random.engmath.dal.ca/rfem/
The parts that are causing the problem are used just for extracting and showing results.
It is an old software so maybe there is some problem with that.
I don't know what exactly you did with your Python code since you didn't provide the exact code snippets. I also don't have the reputation to comment. So I'll just provide code examples for the 3 methods you listed (CMD, Powershell, Python). All three methods worked on my machine.
1) CMD
start "" "plotps.exe" hist.plotps
The double quotes after the start keyword are there to specify an optional title. What went wrong in your CMD example is that windows thought "plotps.exe" was the title. You don't need to specify a title, but you need to write the quotes. More info on this keyword can be found here: https://ss64.com/nt/start.html
Also note that start is asynchronous
A synchronous way of doing it would be:
plotps.exe hist.plotps
2) Powershell
Start-Process -FilePath "plotps.exe" -ArgumentList "hist.plotps"
I would strongly recommend using powershell over CMD if you have access to both.
This method is synchronous.
More info on Start-Process: https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Start-Process?view=powershell-5.0
3) Python
I'm not a python expert but this worked for me:
import subprocess
subprocess.call(['plotps.exe', 'hist.plotps'])
I Want to save my python code as a text file then make a python launcher that once you log in the program starts executing code from the text file. Is This possible to do and if so can someone please help
The request is a bit obscure but if I got it right you want create a python script that is able to launch other python scripts.
I do not understand the use of this but here's a possible solution, keeping in mind that your "text file" was saved as "yourscript.py":
from subprocess import call
call(["python", "yourscript.py"])
This code will act like if you'd run the saved "yourscript.py" from the command line.
Consider using runpy, e.g.:
import runpy
runpy.run_path("yourscript.py", init_globals={'global_name':some_value})
With optional init_globals parameter you can control global objects within the runtime of the script you are calling.
I need to use an old-fashioned DOS/Windows executable (the source is not available). It uses two input files and produces one output file.
I have to run this several thousands times, using different input files. I wrote a simple python script looping over input files to automate this.
The problem is that this exe finishes every single run with the immortal "press Enter".
I start the script, keep the key pressed, 'returns' accumulate in the bufor and the script runs for a while producing several outputs.
Is there any more elegant way to proceed (i.e. without using the finger and staring at the monitor)?
I have already tried some obvious solutions (e.g. os.system('return'), os.system('\n')) but they do not work.
Next day edit:
#Eric, many thanks for the code, it works. I also thank others who contribute, and sorry for slopply written question and unformatted code in the comment (it was 3.30 am :)
From the information in your comment, what I think you want is something like:
import subprocess
for i in range(1, 20001):
command = "wine executable.exe input{number}.txt > output{number}.txt".format(number=i)
p = subprocess.Popen(command, stdin=subprocess.PIPE, shell=True)
# send a newline
p.communicate(input="\n")
Use Python's subprocess module and run your executable with Popen.
Then you can send "enter" to the process with communicate.
Have you tried os.system(‘\r\n’)? I think that’s the newline character on windows.
Edit: Your answer also used a forward slash instead of a backslash--definitely try the other way too, unless that’s just a typo.
I'm fairly new to python programming, i'm familiar with the very basic stuff and i'm currently learning about creating function definitions in scripts.
In particular i'm a mac user and i'm using text wrangler to write and run my python programmes.
Now that i have learned how to define basic functions in scripts i have questions which my notes do not seem to answer.
How do i import my definition which is saved in a file on my desktop to use on IDLE? I've tried
import fileaname
in IDLE and it does not work.
Secondly Suppose i create a function A in one script and then another function B in a separate script that depends on A, do i have to import A in the script for B first? Do they need to be saved in the same file?
I appreciate any advice and useful tips.
With import filename , you want to make sure its in the same directory as your original file. Try using this too
import sys
sys.path.append(directory_path)
It seems to be a common issue.
I'm using Python2.7 and a library called pymidas.
Within my python script I call the library with the following comand:
from pymidas import midas
midas.do('INDISK/FITS test.fits test.bdf')
All the code that I have further written does exactly what I want, but whenever the script imports midas I first get a welcome output of (py)midas, which is ok with me, but afterwards it asks me if I want a parallel or a new session.
Saddly this point needs human interaction in selecting parallel mode. By reading the documentation of midas I found, that midas has an option (-P) which causes exactly what I need, and forces midas to open without any questions asked and directly going to parallel mode.
Does anybody know how to achieve this in my python script?
Thanks!
At the end of your script add :
midas.do('.exit')
This ensures you dont get asked the next time you run the script.