Python 3.9, print(file1.read) with no output [duplicate] - python

This question already has answers here:
Why can't I call read() twice on an open file?
(7 answers)
Closed 1 year ago.
I ve been learning about files in python 3.9.6 when this happen:
I open a file using the open command
Write to a file
printed the text with file.read()
the file.read() type is str
print (file.read()) will return nothing .
I am using python 3.9.6 , pycharm community and python IDLE gave the same result, and the problem is , i assumed that if we passed th file.read() which is a string , the print command will be able to actually print it .
>>>file1 = open('t.txt', 'a')
>>>file1.write('hahah')
>>>file1.close()
>>>file1 = open('t.txt', 'r')
>>>file1.read()
'hahah'
>>>print(file1.read())
>>>type(file1.read())
<class 'str'>

As you can read here: https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
the problem is that you are read the all the file in the first file.read(), which means that you get to the end of the file, so every time (until you will close the file) that you will run file.read() you will get empty string ('').

Related

Writing output of python print() to file appears after process is killed / finished [duplicate]

This question already has answers here:
How can I flush the output of the print function?
(13 answers)
Closed 2 years ago.
I had several print() statements in my code. I want run the code overnight and have them write to file.
I tried redirecting output to file as:
python mycode.py >> log.txt
While python process is running, I cannot see anything written to file. When I kill the process and open the log file, I find the output in it.
Similar behavior was seen when I explicitlys specified file handle in the every print statement:
output_file = open('log.txt','w')
print('blablabla', file=output_file)
How can I make print to write the file immediately? Am I missing anything?
You are not seeing your changes right away because in some cases, due to buffering, changes made to a file may not show until you close the file.
To save changes without closing, please, refer to this page Can I save a text file in python without closing it?
Can you try this and tell me if it updates, immediately or not. I'm always using this syntax in PyCharm. I hope it works with u, too.
If you want to insert bunch of data one at time.
with open("log.txt", 'w') as test:
print ("hello world!", file=test)
If you want to insert data into .txt, then do some process, then back again to add more data to the .txt
test = open("log.txt", 'w')
print ("hello world!", file=test)
#==== Do some processes ====
print("This's the end of the file")
test.close()

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

"print file.read()" Invalid Syntax in Python 3 [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 7 years ago.
I wanted to create a 10 files where each file has a "blob" word at the first sentence and read those sentence directly.
Here's my code:
import random
import string
for i in range(9):
name = input('fileNumber')+ str(i+1) + '.txt'
try:
file = open(name,'w+')
file = open(name,'a')
file.write("blob")
file = open(name,'r')
print file.read() #'file' being highlighted with red color when I execute
file.close()
When I run it, I got an error message saying Invalid syntax and it highlights my file.read() line.
Can somebody tell me where's the flaw in my code?
EDIT: I'm currently using python 3.5. However, I could also switch to 2.7 as well!
Try doing this:
print(file.read())
In Python 3.x print() is a function and the parentheses are mandatory.

How to read text file with 1Ah char in it? [duplicate]

This question already has answers here:
Line reading chokes on 0x1A
(2 answers)
Closed 8 years ago.
I have a file, which is an assembly source with comments. These comments contain 1Ah character. It is a control character, the "substitute", but it also prints a nice right arrow in DOS, so someone long time ago thought it would be a shame not to use it.
Somehow, it works like end of file character for Python. All I do is:
f = open('file.asm')
for line in f:
print line
f.close()
And everything goes ok just until the first entrance of 1Ah.
The question is, how to read this symbol along with other text?
Open the file using universal line ending support:
f = open('file.asm', 'rU')
This avoids opening the file in native-platform text mode (in the C call) and prevents Windows from interpreting the \x1a code point as a line break.
Try:
f = open('file.asm', 'rb')
It should open file in binary mode.

Categories