I am running multiple scripts in sequence according to the list I and the following executable. However, when one of the script in folder (say, 2) runs into an error, it terminates instead of moving to folder 3. Basically I want the executable to move onto the next script if there is an error in the present script. How do I do this?
I=[1,2,3]
for i in I:
exec(open(rf"C:\5100 nodes\{i}\5100_beta_0.01_50.0_1.0ND_3.py").read())
The error encountered while running script in folder 2 is
File "<string>", line 618, in <module>
ValueError: max() arg is an empty sequence
You could use a try-except block.
I=[1,2,3]
for i in I:
try:
exec(open(rf"C:\5100 nodes\{i}\5100_beta_0.01_50.0_1.0ND_3.py").read())
except Error as e:
print(e)
Related
I'm not sure, if this is related to Can't find module when running python script from another python script.
However, if I run my python script directly I have no issues, but when I start it from another script, I get an error:
'Non-existent config key: model.kwargs.backbone_config.kwargs.input_height'
Even though the key can be found if I run the file directly.
However, if I try to execute the following code:
try:
config.merge_from_file('{base}/HoHoNet/config/s2d3d_sem/HOHO_depth_dct_efficienthc_TransEn1_h1024_fold1_resnet101rgb.yaml'.format(base = os.getcwd()))
except Exception as e:
print(e)
return 1
where my config is taken from yacs.config.
I've written a function which reads and runs a python script, then sends it's output to a text file.
I'm trying to get it to write a simple string, or the error in question, to the text file if the script it ran is broken/doesn't work.
Below is the code in question:
file_dir = a_dir[0]
file_name = a_dir[1][:-3]
with open(f'{self.output_directory}\\output_{file_name}.txt', 'w') as f:
try:
subprocess.call(
[sys.executable, file_dir], stdout=f)
except:
f.write("An error occured with the script")
The first part of it works fine - it does run a functioning file and writes the output.
Do I need to be more specific with the error exception? Any help would be greatly appreciated!
If your code works fine and sys.executable is being run then there will be no exception and so your f.write code won't be run. If there is an error in a program you run using subprocess this doesn't propagate to an exception in a program you run it from. You'd have to know something about this program to know that there was an error you could look at the [returncode][1] from the subprocess.call function call. Another option is that instead of running a new python interpreter you could load the module yourself and then run code from within it using try except blocks. If you can't rely on any structure to the file then you could read the file as text and then run the code within it using eval or exec within a try except structure, that being said if you don't know anything about the file in advance it is likely a massive security flaw for you to be executing it at all let alone within the context of your running application.
**late edit read the file as text vice test which was a typo
[1]: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.returncode
I'm setting a bash variable in the main shell by reading the content of a file with
srclang="$(cat ${langdir}/srclang.log)
The value is "en-GB" and it prints just fine.
I then call a function that, in turn, calls a python script, but when I try to access this variable with
os.environ["srclang"]
From the python script, I get the following error
File "/usr/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'srclang'
How can I pass this variable from the function to the python script?
EDIT:
After reading a little more on it, it seems like I have to export it, but exporting is also giving me an error:
export: `en-GB': not a valid identifier
I have a python script that I want to automate so I created a simple bash script (called mybash.sh).
#!/bin/sh
vars="C:\Users\Jane\Desktop\Work\variables.csv"
while IFS="," read gname gid
do
echo "Gene Name: $gname"
echo "Gene ID: $gid"
python alignment.py work_file $gid > C:\\Users\\Jane\\Desktop\\Work_Done\\$gname.fa
done < "$vars"
read -rn1
It is a huge script but I always get an error saying that the last line of my alignment.py script has a NameError, so after running my bash script from windows cmd prompt as >mybash.sh I get this:
Gene Name: cytb
Gene ID: ENSB0010011
Traceback (most recent call last):
File "alignment.py", line 99, in <module>
for species in geneDict:
NameError: name 'geneDict' is not defined
The geneDict is the very last part of the python script which simply creates a fasta alignment file for a gene for all species in separate folder.
When I execute the python script normally in cmd prompt, it works perfectly fine but instead of extracting the gname (gene name) and gid (gene ID) from a variables file, I was typing it manually. I didn't want to do that for 200 genes. I can't understand why it no longer finishes and says that the geneDict is no longer defined?
Also, I tried making the python script run in a bash script alone as follows:
#!/bin/sh
python alignment.py work_file ENSB0010011 > C:\\Users\\Jane\\Desktop\\Work_Done\\cytb.fa
And this worked fine also, the python script did not stop working all of a sudden and my file came out. How do I make the python script execute without error, while pulling the variables from variables.csv separately so that I don't have to type it in manually?
The last bit of the python script is as follows:
for species in geneDict: # this is where it says geneDict is not defined
print '>' + species
print geneDict[species]
NameError exception is raised because global variable "geneDict" is not defined in python script. See https://docs.python.org/2/library/exceptions.html#exceptions.NameError
I am using python 2.4 for a program which imports scripts from the internet and executes them so a script could be changed by the author and the user wouldn't have to re-download the script.
This is the part of the program that downloads the script:
def downloadScript(self,script):
myfile=open('#A file path/'+script['name']+'.txt','w')
try:
downloadedScript=urllib.urlopen(script['location']).read()
except:
#raise error
return
myfile.write(downloadedScript)
myfile.close()
def loadScript(self):
if not self.scriptCurrentlyLoaded:
script=self.scripts[self.scroller.listPos]
if script['location']=='None':
#raise error
return
self.downloadScript(script)
myfile=open('#A file path/'+script['name']+'.txt','r')
for line in myfile:
if line.startswith('from') or line.startswith('import'):
exec(line.strip()) #This was added because of the name errors
#being produced but to no affect
myfile.close()
execfile('#A file path/'+script['name']+'.txt')
self.scriptCurrentlyLoaded=True
self.scriptLoaded=script
else:
#raise error
The very odd thing is that when I run
execfile(script path)
outside of the function, after the script has been downloaded, the script is executed correctly. But trying to run the loadScript function raises name errors in the script even though the names have been imported in the script and before the execfile which I find very odd.
So my question is: Am I using a very bad method to download and execute these scripts?
Sorry if this question was answered before but I can't seem to find anyone else who is trying to run python scripts by downloading them from the internet.
Edit: adding globals as another argument to the execfile has seemed to fix the problem for now. I don't know if any other problems will occur later though.
In R you can simply 'source(url)'. Here's the closest I have found so far in python:
import urllib
(fn,hd) = urllib.urlretrieve('http://host.com/file.py')
execfile(fn)