Python AttributeError: NoneType对象没有属性“close”

时间:2021-08-01 09:03:39

I am learning python and I wrote a script that copies the content of one text file to another.

我正在学习python,并且编写了一个脚本,将一个文本文件的内容复制到另一个文本文件中。

Here is my code.

这是我的代码。

from sys import argv
out_file = open(argv[2], 'w').write(open(argv[1]).read())
out_file.close()

I get the AttributeError listed on the title. Why is it that wen I call the write method on open(argv[2], 'w') the out_file is not assigned a File type?

我得到了标题上列出的AttributeError。为什么我调用open的写方法(argv[2], 'w'), out_file没有指定文件类型?

Thank you in advance

提前谢谢你

2 个解决方案

#1


4  

out_file is being assigned to the return value of the write method, which is None. Break the statement into two:

out_file被分配给write方法的返回值,这是None。把陈述一分为二:

out_file = open(argv[2], 'w')
out_file.write(open(argv[1]).read())
out_file.close()

And really, it'd be preferable to do this:

实际上,最好是这样做:

with open(argv[1]) as in_file, open(argv[2], 'w') as out_file:
    out_file.write(in_file.read())

Using with with statement means Python will automatically close in_file and out_file when execution leaves the with block.

使用语句意味着Python在执行时将自动关闭in_file和out_file。

#2


2  

out_file is bound to the return value of write(); it returns None.

out_file绑定到write()的返回值;它返回None。

The expression open(...).write(...) calls the write method on the open file object but the open file object itself is then discarded again after the expression completes. While the expression is executed only the stack is referencing it.

表达式open(…).write(…)调用open file对象上的写方法,但在表达式完成后,打开的文件对象本身又被丢弃。当表达式执行时,只有堆栈引用它。

You want to use the file object as a context manager instead, and it'll be closed automatically:

您希望使用file对象作为上下文管理器,它将自动关闭:

with open(argv[2], 'w') as writefile, open(argv[1]) as readfile:
    writefile.write(readfile.read())

The with .. as .. statement has also bound just the open file objects to names, so you can now address those objects directly.

与. .作为. .语句还将打开的文件对象绑定到名称,因此现在可以直接处理这些对象。

#1


4  

out_file is being assigned to the return value of the write method, which is None. Break the statement into two:

out_file被分配给write方法的返回值,这是None。把陈述一分为二:

out_file = open(argv[2], 'w')
out_file.write(open(argv[1]).read())
out_file.close()

And really, it'd be preferable to do this:

实际上,最好是这样做:

with open(argv[1]) as in_file, open(argv[2], 'w') as out_file:
    out_file.write(in_file.read())

Using with with statement means Python will automatically close in_file and out_file when execution leaves the with block.

使用语句意味着Python在执行时将自动关闭in_file和out_file。

#2


2  

out_file is bound to the return value of write(); it returns None.

out_file绑定到write()的返回值;它返回None。

The expression open(...).write(...) calls the write method on the open file object but the open file object itself is then discarded again after the expression completes. While the expression is executed only the stack is referencing it.

表达式open(…).write(…)调用open file对象上的写方法,但在表达式完成后,打开的文件对象本身又被丢弃。当表达式执行时,只有堆栈引用它。

You want to use the file object as a context manager instead, and it'll be closed automatically:

您希望使用file对象作为上下文管理器,它将自动关闭:

with open(argv[2], 'w') as writefile, open(argv[1]) as readfile:
    writefile.write(readfile.read())

The with .. as .. statement has also bound just the open file objects to names, so you can now address those objects directly.

与. .作为. .语句还将打开的文件对象绑定到名称,因此现在可以直接处理这些对象。