【问题标题】:How to pass a filename as a function parameter in python?如何在python中将文件名作为函数参数传递?
【发布时间】:2021-05-23 07:49:42
【问题描述】:

我收到以下错误:remove_metadata() 接受 1 个位置参数,但给出了 2 个

class Chess:
    def remove_metadata(filepath):
        with open(filepath, "r", errors="ignore") as f:
            contents = f.read()
            print(contents)
            
def main():
    c1 = Chess()
    c1.remove_metadata('chess_game.pgn')
    
if __name__ == "__main__":
    main()

【问题讨论】:

  • 需要加self,如def remove_metadata(self, filepath):

标签: python file io


【解决方案1】:

将另一个参数self添加到remove_metadata

def remove_metadata(self,filepath):
    with open(filepath, "r", errors="ignore") as f:
        contents = f.read()
        print(contents)

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多