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

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.

Related

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

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 ('').

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

Python: Opening multiple files using 'with open' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: open multiple files using “with open”?
Does, With open() not works with python 2.6
I have seen other answers to help me with this.
But I can't seem to get any of the solutions provided to work at all!
Here is part of my code for it:
with open('Levels.dat', 'w') as l, open('Names.dat', 'w') as n:
I got this error:
with open('Levels.dat', 'w') as l, open('Names.dat', 'w') as n:
^
SyntaxError: invalid syntax
How do I fix this?

SyntaxError: Invalid Syntax in python when trying to use tarfile.open() on a .tgz file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax error on print with Python 3
I want to view the contents of a .tgz file and I found python's tarfile module. I found the following tutorial which looked promising. http://www.doughellmann.com/PyMOTW/tarfile/
Here is my python file below:
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print tar.getnames()
When I actually execute my python file, I get a carrot sign pointing at the 'r' in the last line and the error message: SyntaxError: invalid syntax.
Print is function in python 3.x.
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print(tar.getnames())

Categories