0
if __name__ == '__main__':
    filename = open('sevi.txt', 'wb')
    content = filename.write("Cats are smarter than dogs")
    for line in content.read(): 
        match = re.findall('[A-Z]+', line)
        print match
    filename.close()

I am new to python. I am just opening a file and writing some text into it. Later reading the content find all the characters in it by using regular expression. but I am getting the error as 'NoneType' object has no attribute 'read'. if I use readlines also, I am getting the error.

4
  • TIP: try printing content and see Commented Feb 17, 2016 at 12:59
  • 3
    filename.write() returns None in Python 2. Did you mean to use filename.read() instead? You'll need to open the file in w+ mode to be able to do that though. Commented Feb 17, 2016 at 13:00
  • i am getting the error as None Traceback (most recent call last): File ".\mud.txt", line 12, in <module> for line in content.read(): AttributeError: 'NoneType' object has no attribute 'read' Commented Feb 17, 2016 at 13:01
  • 1
    FWIW, the open function takes a file name string and returns a file object, so calling that file object filename is somewhat confusing. Commented Feb 17, 2016 at 13:10

3 Answers 3

6

The file.write() method returns None in Python 2 (in Python 3 it returns the number of bytes written, for a binary file).

If you want to both write and read with the same file you'll need to open that file in w+ mode, and seek back to put the file position back to the start:

with open('sevi.txt', 'w+b') as fileobj:
    fileobj.write("Cats are smarter than dogs")
    fileobj.seek(0)  # move back to the start
    for line in fileobj: 
        match = re.findall('[A-Z]+', line)
        print match

Note that looping over the file object can be done directly, producing individual lines.

I made two other changes: I renamed your variable to fileobj; you have a file object, not just the name of the file here. And I used the file object as a context manager, so that it is closed automatically even if any errors occur in the block.

Sign up to request clarification or add additional context in comments.

5 Comments

but it is printing as : ['Z', 'Z', 'Z', 'Z', 'Z'] ['Z'] ['X', 'P', 'R', 'C', 'R', 'R', 'R', 'R', 'R', 'C'] ['B', 'E', 'BCDEFGHIH', 'NOPQRSTUK', 'Z', 'N', 'Q', 'T', 'W', 'Z', 'R', 'R', 'R', 'R', 'R', 'R'] ['I'] ['V', 'E', 'R', 'R', 'R', 'R', 'C', 'P', 'Y', 'S', 'C']
@sam: Yes, isn't that what you expected? You didn't tell us anything about what you were trying to do.
@sam: your find_all() call finds all groups of uppercase letters and returns a list of them. So if your text includes only individual Z initials, that's what you get returned.
if i want to search for specifi word in my .txt (example - smarter). then what should i do ? . If i want to find only letters within my .txt.
@sam: that's an entirely new problem, separate from your question, and too broad to answer in comments.
0

filename.write("Cats are smarter than dogs") is the function that returns None type like every function in Python if it's not specified otherwise with a return statement. So the value of the variable content is None and You are trying to read from that. Try filename.read() instead.

Comments

0
import re 

ofile = open('sevi.txt', 'r+')

ofile.write("Cats are smarter than dogs")

ofile.seek(0)

data = ofile.read()

upper = re.findall(r'[A-Z]', data)

print upper

lower = re.findall(r'[a-z]', data)

print lower

ofile.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.