2013-01-25 203 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))