奇怪的Python 3错误

时间:2013-01-25 01:05:32

标签: python debugging python-3.x

所以我用Python编写一个shell,文件名为jsh.py。当我运行代码时,由于某种原因它不起作用。弹出一个窗口但立即关闭。什么是我的代码?

import os
import random
import time
import path
import string
import sys

commandAvail = 'ls where chdir mk mkdir cp mv rm rmdir view ext shutdown edit unix dos man-all man-unix man-dos'
commandAvailUnix = 'exec permission group'
commandAvailDos = ' '
print('Welcome to jsh alpha 1!')
commandPlatform = input('System: (UNIX/DOS):')
commandLine()

def commandLine():
        while 1 == 1:
                command = input('$ ')
                if command in commandAvail:
                        if command.startswith('ls'):
                                commandKeyword = 'ls'
                                commandOptions = command - commandKeyword
                                ls(commandOptions)
                        elif command.startswith('where'):
                                commandKeyword = 'where'
                                commandOptions = command - commandKeyword
                                where()
                        elif command.startswith('chdir'):
                                commandKeyword = 'chdir'
                                commandOptions = command - commandKeyword
                                chdir()
                        elif command.startswith('mk'):
                                commandKeyword = 'mk'
                                commandOptions = command - commandKeyword
                                mk()
                        elif command.startswith('mkdir'):
                                commandKeyword = 'mkdir'
                                commandOptions = command - commandKeyword
                                mkdir()
                        elif command.startswith('cp'):
                                commandKeyword = 'cp'
                                commandOptions = command - commandKeyword
                                cp()
                        elif command.startswith('mv'):
                                commandKeyword = 'mv'
                                commandOptions = command - commandKeyword
                                mv()
                        elif command.startswith('rm'):
                                commandKeyword = 'rm'
                                commandOptions = command - commandKeyword
                                rm()
                        elif command.startswith('rmdir'):
                                commandKeyword = 'rmdir'
                                commandOptions = command - commandKeyword
                                rm()
                        elif command.startswith('view'):
                                commandKeyword = 'view'
                                commandOptions = command - commandKeyword
                                rm()
                        elif command.startswith('edit'):
                                commandKeyword = 'edit'
                                commandOptions = command - commandKeyword
                                edit()
                        elif command == 'man-all':
                                print('Commands that work for all underlying platforms:')
                                print(commandAvail)
                        elif command == 'man-unix':
                                print('Commands that only work on Unix systems:')
                                print(commandAvailUnix)
                        elif command == 'man-dos'
                                print('Commands that only work on DOS systems:')
                                print(commandAvailDos)
                        elif command.startswith('unix'):
                                commandType = 'unix'
                                unix()
                        elif command.startswith('dos'):
                                commandType = 'dos'
                                dos()
                        elif command.startswith('ext'):
                                commandKeyword = 'ext'
                                commandOptions = command - commandKeyword
                                ext()
                        elif command == 'shutdown':
                                sys.quit(0)
                        else:
                                print('jsh has experienced an internal error and has to shutdown.')
                                sys.quit(10)
                else:
                        print('Command \'' + command + '\' not recognized as internal or external.')

def ls(options):
        if commandPlatform == 'UNIX':
                os.system('ls' + options)
        else:
                os.system('dir' + options)
        commandLine()

2 个答案:

答案 0 :(得分:1)

你忘了在elif命令=='man-dos'之后加上':'

运行:python3 yourscript用于调试目的。

答案 1 :(得分:1)

您当前的代码中存在多个错误:

  1. 首先,你有一些混乱的缩进,包括用于缩进的标签和空格的混合。这在Python中非常糟糕,因为不正确的缩进是语法错误。我已经修正了问题中的代码以正确显示(每个标签使用8个空格),但您也需要在文件中修复它。另外,请注意Python约定是每个块缩进四个空格,因此您可能希望将编辑器设置为默认情况下执行此操作。

  2. 其次,您的顶级检查逻辑不正确。您正在测试整个命令字符串是否包含在commandAvail变量中的命令字符串中。如果命令除了命令名之外还有参数,那么这将是错误的。我建议您split命令字符串获取术语列表,然后仅根据可用命令测试第一个术语。而不是进行子字符串搜索,我建议将commandAvail拆分为一组字符串:

    # make a set of commands that we recognize
    commandAvail = set('ls where chdir mk mkdir cp'.split()) # trimmed for space
    
    # then later, test against it
    command = input().split()
    if command[0] in commandAvail: # this is much more efficient!
        # do stuff
    
  3. 最后,您尝试从命令字符串中删除命令本身的方式是错误的。你不能从另一个字符串中减去一个字符串(如果你尝试,你会得到一个TypeError)。幸运的是,我为上一期提出的解决方案在这方面会有所帮助。由于split调用,您将拥有一个术语列表,而不是进行字符串操作。现在,您只需测试第一项(命令),并将其余项作为参数传递。

    事实上,这表明实现比您目前拥有的if / elif块的大链更简单。使用字典在命令字符串和实现它们的函数之间进行映射:

    # map command names to the functions that implement them
    command_dict = {"ls":ls, "where":where, "chdir":chdir} # snipped
    
    # the main loop
    while True:
        # command is first term, the rest go into args
        command, *args = input().split()
    
        try: # use "easier to ask forgiveness than permission" idiom
            command_dict[command](*args) # call function from command_dict
        except KeyError:
            print("Unknown command: {}".format(command))