Shell Not Able Read Files Written with Python
I Made a Python File Which Tells Every. Java File in the Folder It Is Placed in and You Can Select Which Java File to Execute, the Selected Java File's Name Is...
I made a python file which tells every .java file in the folder it is placed in and you can select which java file to execute, the selected java file's name is then written in a file named Input.txt, But i cannot read that file in shell:
My Python File:
import glob, os
import sys
a=sys.path
count=0
os.chdir(a[0])
ifile=open("Input.txt","w")
i=1
for file in glob.glob("*.java"):
if(count%2==0):
print(i,".",file,end="")
g=str(i)
l=len(file)+len(g)
else:
print("".rjust(40-l,' '),i,".",file)
i=i+1
count=count+1
print("")
flag=1
while flag==1:
f=int(input("Enter The File No. You Want To Execute:"))
i=1
for file in glob.glob("*.java"):
if(i==f):
flag=0
x=file
break
i=i+1
if flag==1:
print("File Not Found!!\nPlease Enter Again:")
ifile.write(x)
ifile.close()
The shell file looks like this:
#!/bin/bashs
echo WELCOME TO EXECUTOR
echo Garvit Joshi\(\)
echo USER:$USERNAME
python3 Filename_java.py
filename="Input.txt"
while IFS= read -r line
do
echo "$line"
done <"$filename"
exit
edit 1:Please Do not go through algorithm of python file.It works fine.
1 Answer
Please Do not go through algorithm of python file.It works fine.
Well yes - but actually no. It writes the characters of the filename, but doesn't terminate the string with a newline.
The shell's read command expects to read complete lines, and returns a non-zero exit status if it does not do so. Hence while IFS= read -r line does not execute the loop body.
You should either modify your python code to write the terminating newline, or modify your shell code so that the loop body tests for a non-empty line as well:
while IFS= read -r line || [[ -n $line ]]; do ...
See for example Why does this 'while' loop not recognize the last line?