How can I log a print statement to a file in Python 3.6? - python-3.x

I am going off of this thread.
First, I added import sys to the top of my file. The print statement that I want to log is inside of an if/else statement, specifically in the else portion. It looks like this:
else:
sys.stdout = open('logfile', 'w')
print("hi")
This does create a file called logfile in the same directory as the .py file, but it is emtpy and does not include "hi".

Related

Jupyter Lab - running cell forever with file.close (), print and sys.stdout

I'm not sure but I imagine that there may be issues similar to mine, but I have not found any that has been satisfactory.
When I open my Jupyter Lab and execute a cell as below (code01), it remains with the status of * (see figure01 below) meaning that it is still running the code, but the output of the out1.txt file is printed correctly.
I would like to know if it is normal for the cell to remain running in this circumstances described from code01.
code01:
import sys
file = open('out1.txt', 'a')
sys.stdout = file
print("house")
file.close()
figure01:
Because you redirect the stdout to a file and then close it you are breaking the IPython kernel underneath: there is no way for any stdout to be correctly processed by the kernel afterwards (cannot write to a closed file). You can reproduce it by executing your code in the IPython console rather than in a notebook. To fix this you could rebind the original stdout back:
import sys
file = open('out1.txt', 'a')
stdout = sys.stdout
sys.stdout = file
print("house")
# before close, not after!
sys.stdout = stdout
file.close()
But this is still not 100% safe; you should ideally use context managers instead:
from contextlib import redirect_stdout
with open('out1.txt', 'a') as f:
with redirect_stdout(f):
print('house')
But for this particular case why not to make use the file argument of the print() function?
with open('out1.txt', 'a') as f:
print('house', file=f)

Python 3 does not recognize special characters

This is the app.py file.
#!/usr/bin/python3
import sys
def run():
print(sys.argv)
filename = sys.argv[1]
print(filename)
return
if __name__ == '__main__':
run()
I want to run this code from the command line, so I tried the two following lines each.
python3 app.py input.txt
python3 app.py < input.txt
The first command showed the result I expected, which is ['app.py', 'input.txt']. However the second command just ended up showing ['app.py'].
It seems like the Python code does not recognize the special symbols. How can I make the script recognize them without changing the script itself? i.e. not modifying the command like this: python3 app.py '<' input.txt.
The < character is special and will actually stream the file specified to stdin. You cannot override this behavior as it comes from your shell not python itself. Here is an example of what is really happening, and how you can get the file contents.
import sys
file_contents = sys.stdin.read() # This will read the entire stdin stream into file_contents
This will also work for the | character
echo "Hello, World" | python app.py

Using Path to check if file exists when running script outside of the directory

So I currently use Path to check if a file exists
from pathlib import Path
if Path("main.conf").is_file():
pass
else:
setup_config()
While this works as long as I'm in the directory where I'm running the script, I'd like to make it work whatever directory I'm at and just run the script. I know it doesn't work because it's expecting the main.conf to be in the directory I'm currently in but how do I tell path to only check the in the folder where the script is located in?
You can resolve the absolute path of the script by using sys.argv[0] and then replace the name of that script with the config file to check, eg:
import sys
import pathlib
path = path.Pathlib(sys.argv[0]).resolve()
if path.with_name('main.conf').is_file():
# ...
else:
# ...
Although it seems like you should probably not worry about that check and structure your setup_config so it takes a filename as an argument, eg:
def setup_config(filename):
# use with to open file here
with open(filename) as fin:
# do whatever for config setup
Then wrap your main in a try/except (which'll also cover file not exists/can't open file for other reasons), eg:
path = pathlib.Path(sys.argv[0]).resolve()
try:
setup_config(path.with_name('main.conf'))
except IOError:
pass

Python: NameError from calling a file from the commandline arguments

For an assignment I'm supposed to have to have a line to open a file that is passed as an argument in the commandline, I keep getting
Traceback (most recent call last):
File "execute.py", line 1, in <module>
program=open(programfilename, "r")
NameError: name 'programfilename' is not defined
My code to this point is program=open(programfilename, "r"). I'm not quiet sure what is wrong. It is the first line in my program. Execute.py is the name of my code.
You need to set the programfilename variable to the name/path of the file on a previous line. Alternatively, you could put the filename in quotes instead.
It is the first line in my program
Well there's your problem. You are using programfilename without having defined it first.
Try something like
import sys
programfilename = sys.argv[0] # argument you passed into your program.
program=open(programfilename, "r")
I am not sure what exactly you are trying to.
If you want to call a file using command line, the code can be like this
import sys
with open(sys.argv[1], 'r') as f:
print(f.read())
Run like this
python3 execute.py programfilename
If you want your program to get printed on the console, the code can be like this
import sys
with open(sys.argv[0], 'r') as f:
print(f.read())
This will print the code on the console.
Run like this
python3 execute.py

Testing python programs without using python shell

I would like to easily test my python programs without constantly using the python shell since each time the program is modified you have to quit, re-enter the python shell and import the program again. I am using a 2012 Macbook pro with OSX. I have the following code:
import sys
def read_strings(filename):
with open(filename) as file:
return file.read().split('>')[1:0]
file1 = sys.argv[1]
filename = read_strings(file1)
Essentially I would like to read into and split a txt file containing:
id1>id2>id3>id4
I am entering this into my command line:
pal-nat184-102-127:python_stuff ceb$ python3 program.py string.txt
However when I try the sys.argv approach on the command line my program returns nothing. Is this a good approach to testing code, could anyone point me in the correct direction?
This is what I would like to happen:
pal-nat184-102-127:python_stuff ceb$ python3 program.py string.txt
['id1', 'id2', 'id3', 'id4']
Let's take this a piece at a time:
However when I try the sys.argv approach on the command line my
program returns nothing
The final result of your program is that it writes a string into the variable filename. It's a little strange to have a program "return" a value. Generally, you want a program to print it's something out or save something to a file. I'm guessing it would ease your debugging if you modified your program by adding,
print (filename)
at the end: you'd be able to see the result of your program.
could anyone point me in the correct direction?
One other debugging note: It can be useful to write your .py files so that they can be run both independently at the command line or in a python shell. How you've currently structured your code, this will work semi-poorly. (Starting a shell and then importing your file will cause an error because sys.argv[1] isn't defined.)
A solution to this is to change your the bottom section of your code as follows:
if __name__ == '__main__':
file1 = sys.argv[1]
filename = read_strings(file1)
The if guard at the top says, "If running as a standalone script, then run what's below me. If you imported me from some place else, then do not execute what's below me."
Feel free to follow up below if I misinterpreted your question.
You never do anything with the result of read_strings. Try:
print(read_strings(file1))

Resources