2016-10-17 3 views
1

私はチュートリアルCreate your own shell in Pythonで学んでいます。私は奇妙な問題があります。私は、コードを次のように書いた:Python:[Errno 2]そのようなファイルやディレクトリはありません - 奇妙な問題

import sys 
import shlex 
import os 

SHELL_STATUS_RUN = 1 
SHELL_STATUS_STOP = 0 

def shell_loop(): 
    status = SHELL_STATUS_RUN 

    while status == SHELL_STATUS_RUN: 
     sys.stdout.write('> ') #display a command prompt 
     sys.stdout.flush() 
     cmd = sys.stdin.readline() #read command input 
     cmd_tokens = tokenize(cmd) #tokenize the command input 
     status = execute(cmd_tokens) #execute the command and retrieve new status 

def main(): 
    shell_loop() 

def tokenize(string): 
    return shlex.split(string) 

def execute(cmd_tokens): #execute command 
    os.execvp(cmd_tokens[0], cmd_tokens) #return status indicating to wait for the next command in shell_loop 
    return SHELL_STATUS_RUN 


if __name__ == "__main__": 
    main() 

そして、私は「MKDIRフォルダ」コマンドを入力していたときに、今ではエラーが返されます:[Errno 2] No such file or directoryを。しかし、正しく動作する以前の "help"コマンドを書くと(使用可能なすべてのコマンドが表示されます)、コマンドmkdirは正しく動作し、フォルダを作成します。私のコードに何が問題なのか教えてください。 私は私のリンクのコメントからのコピー&ペーストのWindows 8.1上++メモ帳で64倍

+0

残念ながら、それはあまり効果がありません。同じエラー。 –

+0

それを忘れる:それは正しいです。私の悪い。あなたはexecvpを呼び出す前に 'cmd_tokens'を印刷できますか(あなたの質問に入れてください) –

+0

あなたのリンクのコメントを読む –

答えて

0

を書いて、あなたがWindows上でそれを試みたよう

Ari Goldためのおかげで)TYHこんにちは、それはそうです。 (LinuxやMac、またはCygwinのようなUnixライクなエミュレータで動作することに注意してください)

最初の問題は、システム環境でmkdirコマンドが見つからないようです。 mkdirバイナリが存在するディレクトリがあり、execvpe()を使用して明示的に環境を指定することがあります。

Windowsのosモジュールにはfork()関数がありません。 しかし、Windows上でCygwinを使用してUnixのような環境をエミュレートすることをお勧めします。上記の2つの問題はなくなります。

Windows 10にはLinux-Bashがありますが、うまくいくかもしれませんが、決して試してみません。

関連する問題