Python &引用;Can';t分配给函数调用";使用;加上;

Python &引用;Can';t分配给函数调用";使用;加上;,python,python-3.x,with-statement,Python,Python 3.x,With Statement,我正在尝试编写一个脚本,该脚本将读取目录中的所有文件,并将它们转储到单个文件中。我得到的是: from glob import glob directory = glob('/Users/jmanley/Desktop/Table/*') with outfile as open('/Users/jmanley/Desktop/Table.sql', 'wb'): for file in directory: with readfile as open(file, '

我正在尝试编写一个脚本,该脚本将读取目录中的所有文件,并将它们转储到单个文件中。我得到的是:

from glob import glob

directory = glob('/Users/jmanley/Desktop/Table/*')

with outfile as open('/Users/jmanley/Desktop/Table.sql', 'wb'):
    for file in directory:
        with readfile as open(file, 'rb'):
                        outfile.write(readfile.read())
我得到了
“无法分配给函数调用”
作为错误消息,IDLE用关键字标记
,作为错误的位置

如果我将脚本重写为使用
open()
close()
方法,而不是使用
with
关键字,那么它将正常运行:

from glob import glob

directory = glob('/Users/jmanley/Desktop/Table/*')
outfile = open('/Users/jmanley/Desktop/Table.sql', 'wb')

for file in directory:
    readfile = open(file, 'rb')
    outfile.write(readfile.read())
    readfile.close()

outfile.close()
为什么会出现
“无法分配给函数调用”
错误?我唯一一次看到这种情况发生是如果赋值被颠倒:
a+b=variable
。我只是错过了一些非常明显的东西吗?

请注意:

with foo as bar:
(非常非常粗略地)相当于:

bar = foo
(这与Python中
as
的其他用法一致,例如
除了ValueError as err:

因此,当您尝试:

with outfile as open('/Users/jmanley/Desktop/Table.sql', 'wb'):
您实际上正在尝试分配:

open('/Users/jmanley/Desktop/Table.sql', 'wb') = outfile
这显然是错误的。相反,您需要反转该语句:

with open('/Users/jmanley/Desktop/Table.sql', 'wb') as outfile:
另请参见。

注意:

with foo as bar:
(非常非常粗略地)相当于:

bar = foo
(这与Python中
as
的其他用法一致,例如
除了ValueError as err:

因此,当您尝试:

with outfile as open('/Users/jmanley/Desktop/Table.sql', 'wb'):
您实际上正在尝试分配:

open('/Users/jmanley/Desktop/Table.sql', 'wb') = outfile
这显然是错误的。相反,您需要反转该语句:

with open('/Users/jmanley/Desktop/Table.sql', 'wb') as outfile:
另请参见。

注意:

with foo as bar:
(非常非常粗略地)相当于:

bar = foo
(这与Python中
as
的其他用法一致,例如
除了ValueError as err:

因此,当您尝试:

with outfile as open('/Users/jmanley/Desktop/Table.sql', 'wb'):
您实际上正在尝试分配:

open('/Users/jmanley/Desktop/Table.sql', 'wb') = outfile
这显然是错误的。相反,您需要反转该语句:

with open('/Users/jmanley/Desktop/Table.sql', 'wb') as outfile:
另请参见。

注意:

with foo as bar:
(非常非常粗略地)相当于:

bar = foo
(这与Python中
as
的其他用法一致,例如
除了ValueError as err:

因此,当您尝试:

with outfile as open('/Users/jmanley/Desktop/Table.sql', 'wb'):
您实际上正在尝试分配:

open('/Users/jmanley/Desktop/Table.sql', 'wb') = outfile
这显然是错误的。相反,您需要反转该语句:

with open('/Users/jmanley/Desktop/Table.sql', 'wb') as outfile:

另请参见。

以open('/Users/jmanley/Desktop/Table.sql',wb')作为输出文件:
…应
以open('/Users/jmanley/Desktop/Table.sql',wb')作为输出文件:
…应
以open('/Users/jmanley/Desktop/Table.sql',wb')作为输出文件:
…应
以open作为输出文件('/Users/jmanley/Desktop/Table.sql',wb')作为输出文件:
。。。