Calling a python script from Linux cuases TypeError: file() takes at most 3 arguments (4 given)

Calling a python script from Linux cuases TypeError: file() takes at most 3 arguments (4 given)

这个脚本已经运行了很多次(我是第一次使用它),所以我相信 Linux 中的命令不知何故是错误的,即使我检查了很多次。

命令需要具有以下结构:

python python_script_path.py -ccdb db_file.db -csvNameResolution input_csv_file.csv -csvManual another_input.csv -csvOut output_file.csv

python脚本

# imports

parser = argparse.ArgumentParser(description='usage:')
parser.add_argument('-ccdb','--ccdb', help='input: file to read from',required=True)
parser.add_argument('-csvNameResolution','--csvNameResolution',help='input: csv file - the output of the name resolution script', required=True)
parser.add_argument('-csvManual','--csvManual',help='input: csv file - manually prepared', required=True)
parser.add_argument('-csvOut','--csvOut',help='output: csv file which is the merge of the 2 inputs plus data queried', required=True)
args = parser.parse_args()

myEncoding = 'utf-8'

sys.stdout = open(sys.stdout.fileno(), mode='w', encoding=myEncoding, buffering=1) # That's wehre the error occurs.

错误信息:

Traceback (most recent call last): File "python_script_path.py", line 39, in sys.stdout = open(sys.stdout.fileno(), mode='w', encoding=myEncoding, buffering=1) TypeError: file() takes at most 3 arguments (4 given)

我不明白这条消息有什么问题。我查看了具有类似错误消息的问题,但此错误出现在各种不相关的场景中。

谢谢!

看起来好像代码是为 Python 3 编写的,但您使用 Python 2 调用它。在 Python 3 中,open() 的签名更改为包括各种其他参数,包括此脚本使用的 encoding。尝试 运行:

python3 python_script_path.py -ccdb db_file.db -csvNameResolution input_csv_file.csv -csvManual another_input.csv -csvOut output_file.csv