Python - print() - debugging -show file and line number [duplicate] - python

This question already has answers here:
filename and line number of Python script
(11 answers)
Closed 5 years ago.
When using print() in python, is it possible to print where it was called? So the output will look like var_dump in php with xdebug. Eg. I have script D:\Something\script.py, and at line 50, there is a print("sometest"), so the output will look like this:
D:\Somethinq\script.py:50 sometest
Or is there any module that could achieve this? In large projects, it's really hard to manage where these prints came from.

So, using answers provided in filename and line number of python script , this function can be called instead of print(), and prints line number before every output:
from inspect import currentframe
def debug_print(arg):
frameinfo = currentframe()
print(frameinfo.f_back.f_lineno,":",arg)

Related

Can't get to write output in a file [duplicate]

This question already has answers here:
Redirect stdout to a file in Python?
(14 answers)
Closed 26 days ago.
The following code is should write the line "Hello World" in a output.txt file but doesn't, how do i correct it if its wrong?
import sys,os
if not os.environ.get("ONLINE_JUDGE"):
sys.stdin=open('./input.txt','r')
sys.stdout=open('./output.txt','w')
import time
start_time=time.time()
print("Hello World")
print("______ %s seconds ____"%(time.time()-start_time))
The following will give you write/read capability for .txt files, with the added benefit of not requiring any import statements, and is, in my opinion, easier to use.
with open("{0}.txt".format(fullfilepath), mode = 'w+') as output:
output.write("Hello World!\n")

why python read informaton in file but not it's content? [duplicate]

This question already has an answer here:
Python Read File Content [duplicate]
(1 answer)
Closed 2 years ago.
I try to Python read and then print text from file score.txt (in score.txt is text hrllo world) i write this command:
score = open("data/score.txt", "r")
print(score)
and output is:
<_io.TextIOWrapper name='data/score.txt' mode='r' encoding='cp1250'>
how can i print "hello world" from file score.txt?
You probably want to read the whole filo into the variable in your case.
score = open("data/score.txt", "r").read()
See https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
I also offer some unsolicited advice: I recommend using a so-called context manager which will automatically close the file after you're done using it (even in case reading the file fails for some reason).
with open("data/score.txt", "r") as score_file:
print(score_file.read())
This is not really very important in your case, but it is an accepted best practice and should be followed whenever possible.

Python3 reading and writing .txt files [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 5 years ago.
I'm somewhat new to Python, but I have enough under my belt to know what I'm doing. What I'm trying to do is write a few lines for a .txt file (as well as a variable), and then print 5 of those characters.
import os
username = "Chad_Wigglybutt"
file = open("testfile.txt", "w")
file.write("Hello .txt file, ")
file.write("This is a test, ")
file.write("Can this write variables? ")
file.write("Lets see: ")
file.write(username)
file.close()
It then creates the file without issue, but when I add
print file.read(5)
to the code, it gives me a syntax error for file.read, and I have no clue why. I've been on the internet for a few hours now and I can't find anything. Either I'm extremely bad at google searching and I'm an idiot, or something's broken, or both. Any tips/ideas? :/
You're writing Python 3 code. In Python 3, print is a function, not a special statement. You need parentheses for function calls:
print(file.read(5))

Python Profiler garbage [duplicate]

This question already has answers here:
cProfile saving data to file causes jumbles of characters
(3 answers)
Closed 6 years ago.
I am currently profiling some python code like so:
import cProfile
cProfile.runctx('self._Foo ()', globals(), locals(), 'c:/restats.txt')
def Foo(self):
stuff
The code runs successfuly, outputs no exception. However my restats.txt looks like this:
SomeFunc( i i gÇZ3a/­?gÇZ3a/­?0( s; C:\SomeFolder\bar.pyiL t
Basically it is 150 lines of garbage characters with random paths and function names mixed in.
What could be the issue? Am I using this correctly? The python file is being loaded in through Maya if that makes a differance.
Please read the documentation for what to do after you have collected the trace information. I believe you will need to use pstats.Stats to extract the information you need.
The file is not meant to be human-readable. As described in the documentation, you can use the pstats module to load the file and explore the profiling data.

How to check if command line argument is file or not in python? [duplicate]

This question already has answers here:
How do I check whether a file exists without exceptions?
(40 answers)
Closed 7 years ago.
I want to give input as
%>Python_script_name listed_files
How to check if exist as file or it is some input argument.
<listed file can also be in path /govr/rest/listed_files >
Please share the small script, by which I can check if command line argument is valid file or it is a simple variable to be processed.
Just to summarize, I need to process my script so as to take or a single argument at command line.
Please help.
Thanks in advance.
Short and simple Answer.
import os.path
os.path.isfile(fname)

Categories