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.
我是python的新手。我只是打开一个文件,然后写一些文本进去。之后,通过使用正则表达式来读取内容,找到其中的所有字符。但我将错误理解为'NoneType'对象没有属性'read'。如果我使用readlines,我也会得到错误。
3 个解决方案
#1
6
The file.write()
method returns None
in Python 2 (in Python 3 it returns the number of bytes written, for a binary file).
write()方法在python2中没有返回(在python3中,它返回所写的二进制文件的字节数)。
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:
如果你想用相同的文件来写和读,你需要在w+模式下打开这个文件,然后返回到开始的文件位置:
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.
我还做了另外两个更改:我将变量重命名为fileobj;您有一个文件对象,而不仅仅是文件的名称。我使用file对象作为上下文管理器,因此即使在块中出现任何错误,它也会自动关闭。
#2
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.
文件名。write(“猫比狗更聪明”)是一个函数,如果没有指定返回语句,它返回的函数不会像Python中的每个函数一样。所以变量内容的值是None,你要从中读取。尝试filename.read()。
#3
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()
#1
6
The file.write()
method returns None
in Python 2 (in Python 3 it returns the number of bytes written, for a binary file).
write()方法在python2中没有返回(在python3中,它返回所写的二进制文件的字节数)。
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:
如果你想用相同的文件来写和读,你需要在w+模式下打开这个文件,然后返回到开始的文件位置:
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.
我还做了另外两个更改:我将变量重命名为fileobj;您有一个文件对象,而不仅仅是文件的名称。我使用file对象作为上下文管理器,因此即使在块中出现任何错误,它也会自动关闭。
#2
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.
文件名。write(“猫比狗更聪明”)是一个函数,如果没有指定返回语句,它返回的函数不会像Python中的每个函数一样。所以变量内容的值是None,你要从中读取。尝试filename.read()。
#3
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()