2013-01-25 172 views
-2

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

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() 
+0

是你的代码缩进* *正是这样? –

+2

尝试从控制台运行脚本,而不是直接运行脚本。你应该得到一个有更多信息的回溯。 – Wessie

+0

操作系统? –

回答

1

你忘了把 ':' ELIF命令后== '男人-DOS'

只要运行:python3 yourscript进行调试。

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))