次の例を検討してください
#junk path ending with a test file
test = "C:\\test1/test2\test3.txt"
withimport os
とos.path.abspath
in はパス名を正規化できます
test_norm = os.path.abspath(test)
print test_norm
C:\\test1\\test2\test3.txt'
パス名を分割するとos.path.split
、次の問題があります
os.path.split(test_norm)
('C:\\test1', 'test2\test3.txt')
それ以外の
C:\\test1\\test2 and test3.txt
この問題は、ユーザーinput_raw
が例としてディレクトリを入力したことが原因です。でこれを回避できますraw_input
か?