Python IOError: [Errno 5] Input/output error? - python

I m running on a remote server a python script using nohup.
First I connected to the remote machine using a VPN and SSH
Second I run a python script using the following command:
nohup python backmap.py mpirun -np 48 &
The python script contains the following lines:
frame = []
file_in = open("Traj_equil_prot.pdb", "r")
for line in file_in:
if line.startswith('TITLE'):
frame.append(line[127:134])
import os
for fileNum in range(631, 29969):
os.system("./initram-v5.sh -f Traj_equil_prot_frame" + str(fileNum) + ".pdb -o Traj_equilprot_aa_frame" + str(frame[fileNum]) + ".gro -to amber -p topol.top")
The script was running just fine the whole day. but now it just crashed and when I try to re-launch it again I'm getting the following error:
Traceback (most recent call last): File "", line 1, in
IOError: [Errno 5] Input/output error
The file is in the working directory. I tried to disconnect/connect again but still the same problem. I don't know what I'm missing. Any help, please?

I had the same problem, I used to run my script using this command:
python MY_SCRIPT.py &
The script will run in the background and the output will be displayed on the terminal until you logout or exit.
By logging out, the script is still running but it's output has nowhere to display thus, exceptions will be made when the script wants to display something (e.g. calling print).
Solution:
I've piped the output to somewhere other than the actual terminal display:
python MY_SCRIPT.py >/dev/null &
Checkout this link for more details:
https://stackoverflow.com/a/38238281/6826476

I finally fixed the problem by opening the file "file_in", modifying it (just adding a point in the REMARK line for example) and saving the changes.

Related

Apache Permission Error on open file in Python

I am new to web design, and I'm trying to setup an Apache web interface to some python code.
I am running a test python script that just opens a new file in /var/www/html/ called output.txt and attempts to write to it. When the web page is loaded, I use shell_exec('/usr/bin/python3 /var/www/html/ptest.py'); to run my python script. The python script does run and outputs some print messages, but trying to open output.txt fails with:
Traceback (most recent call last): File "/var/www/html/ptest.py", line 8, in f=open("/var/www/html/output.txt", "w") PermissionError: [Errno 13] Permission denied: '/var/www/html/output.txt'. It will also fail if the file already exists and I open to read.
I have confirmed that the script is being run by apache with getpass.getuser(), and I have tried as many different permission combinations as I can think of for /var/www/html/, including 777. I have set apache as the group for every directory from /var/ to html/. I have tried creating the file ahead of time with 777 permissions. I have checked that /var/ to html/ has group execution permission.
I have tried creating and using another folder completely owned by apache.
I have looked through the apache directives to see if there is a directive I need to have, but I haven't found one yet.
I've included the code for my python script and my php page below.
EDIT: I've tried running ptest.py as apache using su -s /bin/bash apache.
ptest.py successfully runs this way, so the problem doesn't seem to be a permission associated with the apache user/group.
ptest.py
#!/usr/bin/python3
import sys
import getpass
import os
sys.stderr = sys.stdout
print(getpass.getuser())
print(os.getgid())
f = open("/var/www/html/output.txt", "w")
f.write("banana")
f.close()
print("I wrote banana, which is a berry")
banana.php
<html>
<head></head>
<body>
<h2>Welcome to the Program Test</h2>
<?php
echo "Created Command", "<br>";
$output = shell_exec('/usr/bin/python3 /var/www/html/ptest.py');
echo "Executed Command", "<br>";
echo $output, "<br>";
echo "End of output", "<br>";
$output = shell_exec('ls /var/www/html/');
echo $output;
?>
</body>
</html>
Credit to domino_pl leaving a comment.
It looks like typical selinux problem. Try selinux setenforce 0 – domino_pl

redirecting the output of a python command to a txt file working in cmd but not in .bat file, how to solve?

well I'm trying to make a logger for my python script using a runner in a '.bat' format, that executes the script and saves an output file; without me having to do it manually.
when I tried to run my python script, script.py, and pass 20 as an argument for the script as well as redirecting the output to a log_file.txt, using windows command prompt, it worked just fine, and the log file was created.
~the cmd command:
python script.py 20 >> log_file.txt
But when I tried to run the same code using the runner ".bat" file it didn't work.
~The codes I've written inside the "runner.bat" is as follows
python script.py 20 >> log_file.txt
pause
~but the execution command is done by the bat file was-as seen from the screen-:
C:\Users\dahom\Desktop\folder>python script.py 1>>log_file.txt
I expected the ".bat" file to behave the same save the log_file as the cmd terminal.
But when I ran the bat file it didn't redirect the output to the log_file.txt
But it seems to be running the script, without but one indication that it takes some time for the script running.
note: both the batch file and the script are in the same folder/dir/path.
HERE is an image showing everything.
TRY:
#echo off
"C:\Users\dahom\AppData\Local\Programs\Python\Python37-32\python.exe" "C:\Users\dahom\Desktop\new.py" >> "C:\Users\dahom\Desktop\log_file.txt" & type "C:\Users\dahom\Desktop\log_file.txt"
pause
NEW.py:-
print("Echo Fox")
OUTPUT OF THE BATCH SCRIPT:-
Echo fox
Press any key to continue . . .
WORKING:-
Just Provide the full paths of each file used in the command (python exec, python script, text file etc). When the command get's pipe'd to file use & type "file_path" to display the contents of the file after writing it.

Python script functions correctly except when I try to call it from a bash script

I am creating a bash script, for Mac OS, to run several python scripts in sequence. Right now I am just trying to get the bash script to run one python script and have been unsuccessful.
#!/bin/bash
python3 /Users/johndoe/Documents/Projects/Test.py
I made the bash file executable with chmod 700, and gave the gatekeeper permission to run it. Here is what it says when I run the script:
MacBook:~ JohnDoe$ /Users/JohnDoe/Documents/Projects/Bscript ; exit;
Traceback (most recent call last):
File "/Users/JohnDoe/Documents/Projects/Test.py", line 19, in <module>
with open('Data/' + filename + '.json', 'w') as outfile:
FileNotFoundError: [Errno 2] No such file or directory: 'Data/20180105-234709.json'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
This python script works just as intended when running from the terminal manually, but the Bash script gives me an error about the with open line. Does anyone have an idea whats going on? It appears that it is failing to create a new file... but why is that causing it to fail if that is the case?
For background the Test.py is pulling Json data from internet and saving it to a new file it creates.
Thank you.

Running command line program using Django

Im trying to execute a command line programa through Django using the subprocess.check_output(). I have tried to do it first with simple commands like:
subprocess.check_output('ls', '-l')
And its working ok, but now I'm trying to do it with a command line program, which I have already put in the root folder of my project (so if I execute a 'ls -l' it appears there) but Django is throwing me an 'OSError: [Errno 2] No such file or directory'
The programs needs to be in somewhere particularly? This is how I'm doing it right now:
output = subprocess.check_output(['kmersFreq', 'sequence.fasta', '2', '0'])
print output
add shell = True argument in your subprocess call

Why do I get 'Bad file descriptor' when trying sys.stdin.read() in subversion pre-revprop-change py script?

I'm trying pre-revprop-change hook script on a Windows machine, but find a tough problem.
I distill my problem to the following scenario:
I have C:\csvn\data\repositories\tr1\hooks\pre-revprop-change.bat with content:
D:\svntest\testhook.py %*
exit %ERRORLEVEL%
testhook.py is:
import os, sys
if __name__ == '__main__':
# sys.stderr.write(sys.version+'\n') # this is OK, tried.
newtext = sys.stdin.read() # try to read new log message
sys.stderr.write('newtext is: %s\n'%newtext)
exit(2)
However, when doing a client-side svn propset svn:log --revprop -r 2 "newtext" , I got python error:
Traceback (most recent call last):
File "D:\svntest\testhook.py", line 5, in <module>
newtext = sys.stdin.read() # try to read new log message
IOError: [Errno 9] Bad file descriptor
What's the cause of this error?
It seems that pre-revprop-change.bat does not pass STDIN handle to the py script. Yes, I verified that pre-revprop-change.bat can fetch text from STDIN(according to instructions in Read stdin stream in a batch file).
I also tried to run pre-revprop-change.bat directly from command line, sys.stdin.read() is OK.
Please kindly help me out.
Screen shot below:
Env:
Windows Server 2003
Collabnet Subversion Edge 2.3(svn 1.7.3 and Apache 2.2.22)
Install Python 2.7.1 msi from python.org
===============[LATEST UPDATE]======================
I'm sorry to say: I should have written in the .bat
exit %ERRORLEVEL%
instead of
exit /b %ERRORLEVEL%
For someone who have tried it with me, please fix it and try again. The /b seems to have pre-revprop-change.bat always exit with 0. TIP: Without /b, running the .bat directly from a cmd window will cause the cmd window to close, so we'd better try it with cmd /c "pre-revprop-change.bat some param".
Quick way to reproduce this problem below
What's more, for those who is still interested, please
download this file package http://down.nlscan.com/misc/chjsvnpyhook.zip ,
extract them to D:\ ,
cd into D:\svntest\tr1_local ,
run elog.bat (svn propset svn:log --revprop -r 2 "newtext")
then my problem will be reproduced. (Requirement: svn.exe 1.7 command line(whether collabnet or TortoiseSVN), and Python 2.7 installed)
If you still cannot reproduce the error. I've prepared a VMware virtual machine that exhibits the problem exactly. Download the VM at http://down.nlscan.com/misc/chj/winxp-svnhook-py-stdin-error.7z (link expected to be valid until Sep 2013). VMware Player 3.0(free) is sufficient to run that VM.
===============[WORKAROUND FOUND]===================
Very nice workaround provided by #nmenezes, with c:\Python27\python.exe D:\svntest\testhook.py %* in bat .
SVN is executing the script without an associated console.
So you won't see anything displayed on STDOUT.
Everything sent to STDERR is displayed as a message on SVN, if the script returns an error code different of 0.
This kind of script should run unattended, it is independent of input or output from the user.
You can try to pass the property value as an extra command line parameter.
To do so, change your .bat to:
#echo off
set /p NEWTEXT=
test.py %* %NEWTEXT%
exit /b %ERRORLEVEL%
And the .py to:
import os, sys
if __name__ == '__main__':
newtext = sys.argv[6]
sys.stderr.write('newtext is: %s\n'% newtext)
exit(2)
I read the batch STDIN to NEWTEXT variable and I pass it to your script as an extra command line parameter.
This solution does not work for multiple lines values. So, I tried again your original solutions and it worked fine.
svn propset svn:log --revprop -r 3 -F svn.txt
In this case, the property value is read from the svn.txt file, a text file with multiple lines. In this case, the option with set /p does not work, as we discussed in the comments. But the original script works.
For information, 5 properties are passed on the command line:
1 - repository
2 - revision number
3 - user
4 - property name
5 - operation (M in this case)
The property value is passed on the stdin of the batch script.
#Chen, I finally downloaded the image. The problem is solved when you change the hook batch to:
c:\python27\python.exe d:\svntest\testhook.py %*
exit %ERRORLEVEL%
It looks that the way your XP machine executes python directly is misconfigured.
Isn't it up to your batch file to direct the stdin it received into the script that it calls?
For reference: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
Maybe you can just prefix the call to your script with the > symbol to have this happen?
> D:\svntest\testhook.py %*
exit /b %ERRORLEVEL%
It seems like if the batch file can read from stdin, then SVN is doing what it is supposed to and it is up to your batch file to make that available to the additional script that you call.

Categories

Resources