Python Forum

Full Version: Showing and saving the output of a python file run through bash
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I'm new to the world of Python, linux and programming in general so my question may be very basic but I can't find an answer anywhere.

I'm running a python file (to do a regression so I'm expecting tables as result) on a virtual cluster using a shell file. The job launches and runs fine but I can't visualize the result (print them on prompt) nor create a file with the output.

What could I do to see the results of my python file? Is there a command to put in the bash or in my Python code?

I've already tried a couple of solutions that did not work like putting the following command in the bash python
mycode.py > SomeFile.txt 
This generated an empty txt file named SomeFile.

I also tried adding the following lines in my Python code to get the results in tex and pickle files but it didn't work either:

print(f"Output file: {results.data.htmlFileName}")
results.writeLaTeX()
print(f'LaTeX file: {results.data.latexFileName}')
results.writePickle()
print(f'Pickle file: {results.data.pickleFileName}')
Thank you in advance for any help Smile

PS: To have a better idea of the Python code that I want to run on the cluster I attached the script mycode.py (small clarification: since it is the first code I run so as a test I only ask for 5 Monte Carlo draws but the objective is to ask for 100000 draws or more)
You should tell Python to run the file, so from bash one can do:

python my_program.py > my_program_output.txt
From within Python one can print to file and write to file.
Thank you perfringo!

That is exactly what I did. I just forgot to put it in my question here.

The resulting txt file is empty :/
why not test it with a simple example. Create a script "hello.py" with one print statement like print("Hello World").
On bash (the line starts with a $) go to the directory of your script and type (the interpreters name is python or python3):
python3 hello.py
You should either see "Hello World" or some kind of error.