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

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")

Related

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.

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

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)

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))

Why when importing base64 in python my prints are done twice (x2)? [duplicate]

This question already has answers here:
Why is this Python code running twice? [duplicate]
(2 answers)
Closed 6 years ago.
import base64
image_base64 = 'Hello World\n'
print image_base64
print 'Hello?\n'
Result:
Hello World
Hello?
Hello World
Hello?
Your file must be named base64.py so when you import base64 at the top of the file, it is importing itself causing the print statements to execute twice (once on the import and once afterwards).
You should rename your script to something whose name does not conflict with the name of a standard module.

using python subprocess's call function, but not getting output file [duplicate]

This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Python: How do I redirect this output?
(2 answers)
Closed 8 years ago.
I'm using python subprocess library to run command line in python file.
After importing the library, I used following code to store the output
call(["python", "make.py", ">", "data"])
But for some reason, I didn't get data file
You have to modify the stdout , check the official document subprocess
import subprocess
my_output_file = open("/home/user/output", "a")
subprocess.call(["python", "hello.py"],stdout=my_output_file)
my_output_file.close()

Categories