【问题标题】:Error in def_main(), excecute takes 1 positional argument, but 2 was givendef main() 中的错误,执行需要 1 个位置参数,但给出了 2 个
【发布时间】:2017-11-30 10:37:16
【问题描述】:
import shlex
import os
os.environ['SERVER_SOFTWARE'] = 'Dev'
import subprocess, socket, time, struct

def execute_command(cmd):
        f = open(log.name, "w")
        try:
            subprocess.Popen(cmd, 0, f, f) 
        except WindowsError:
            cmd[0] = cmd[0] + ".com"
            subprocess.Popen(cmd, 0, f, f) #work-around
        rc = process.wait()
        if rc != 0:
            print("Error: failed to execute command:", cmd)
            print(error)
        return result

def main():
        execute_command(["tree", "C:\\"], "treelog.txt")
        return 

【问题讨论】:

  • def_main(): 似乎是一个错字。你的意思可能是def main(): 如果你要编程,你需要提高对细节的关注。
  • 另外,请正确格式化您的代码。这是不可读的。
  • 你能展示你只将一个参数传递给execute_command的版本吗?传递两个参数是一个错误是显而易见的。
  • 我只改了def main(),像这样:excecute_command(["tree"], "treelog.txt") return

标签: python


【解决方案1】:

错误消息非常清楚,我很惊讶您需要任何解释:您定义了一个函数execute_command(cmd),它接受一个参数(cmd),然后用两个参数调用它。

您的代码中还有很多其他问题,但是由于您从未费心格式化代码并编写真正的问题(复制/粘贴错误消息的部分作为标题并不算作“真正的问题” ),我也懒得指点他们了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多