1
ファイルをランダムに選択して実行するかどうかを尋ねるスクリプトを作成しています。私は最近、以下に示すエラーを受け取りました。これを修正するためにスクリプトを変更する方法については混乱しています。ファイル名でファイル名を印刷しようとするとcp932エンコーディングエラーを処理する方法
Traceback (most recent call last):
File ".\randomplay.py", line 33, in <module>
print(file)
UnicodeEncodeError: 'cp932' codec can't encode character '\u8138' in position 1: illegal multibyte sequence
以下のコードはPython 2.xで動作します。しかし、私はPython 3.xでこの関数を実装する必要があります。誰も私にそれをする方法を助言することはできますか?以前と同じように動作させたい@eryksun
ことで
import sys
import os,io
import random
import subprocess
import msvcrt
if __name__ == '__main__':
indir = os.path.realpath("./")
for root, dirs, files in os.walk(indir):
random.shuffle(files)
#print(files)
for file in files:
cmd="\""+file+"\""
print(file)
print("OK with it?")
c = msvcrt.getch()
print(str(ord(c)))
if ord(c) == 27:
exit()
elif ord(c) == 13:
print("executing...")
subprocess.Popen(cmd,shell=True)
exit()
else:
print("How about this?")
pass
私の悪い、私は今 –
あなたはそれが実際にはPython 2で動作、または 'に置き換えた文字とファイル名を返すos.walk'んよろしいですそれを編集「?」または "・"文字ですか? Python 2のデフォルトは、従来のANSIファイルシステム関数です。 ANSI APIは、サポートされていない文字に対して不可逆エンコードを使用します。 Python 2でオリジナルのUnicodeファイル名を表示するには、 'unicode'パスを使用する必要があります。 'os.walk(u"。 ")'。オリジナルのUnicode文字を印刷するための – eryksun
簡単な解決策は、コンソールのUTF-16 APIを使用して、デフォルトのPython 3.6にアップグレードすることです。やPython 2を含む古いバージョン、について、 'win_unicode_console'モジュールをインストールし、有効PIP。 – eryksun