2

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!

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
coava
  • 79
  • 3
  • 9

1 Answers1

5

Try doing this:

print(file.read())

In Python 3.x print() is a function and the parentheses are mandatory.

Óscar López
  • 232,561
  • 37
  • 312
  • 386