2013-12-08 28 views
1

爲什麼Python的追加*串

>>> import os 
>>> os.listdir('C:\\Users\\tom\\Desktop\\PythonScripts\\charList.txt') 

給這個錯誤:

Traceback (most recent call last): 
    File "<interactive input>", line 1, in <module> 
WindowsError: [Error 267] The directory name is invalid: 'C:\\Users\\tom\\Desktop\\PythonScripts\\charList.txt/*.*' 

我認爲這是與字符串編碼的問題,但顯然不是?

回答

5

這裏:'C:\Users\tom\Desktop\PythonScripts\charList.txt'。這是一個文件名,而不是目錄名稱

嘗試沒有charList.txt

os.listdir('C:\\Users\\tom\\Desktop\\PythonScripts') 

爲什麼要追加的文件名到listdir操作有什麼特別的原因嗎?

+0

其中存在問題。看起來我需要去喝咖啡。它從來沒有發生過,但它現在完全合理!你先生太棒了!非常感謝!! 顯然我必須等待9分鐘才能選擇正確答案... –

+0

@LokiIkol休息一下。發生這種情況;)哈哈。不客氣 –

+0

我不能在工作中「休息」。但我可以從事其他工作,比如刷新我的Python技能。 > :) –

0
listdir(path) -> list_of_strings 

Return a list containing the names of the entries in the directory. 

path: path of directory to list 

The list is in arbitrary order. It does not include the special 
entries '.' and '..' even if they are present in the directory. 

你已經給它一個文件路徑,而不是目錄。

+0

很好的解釋。我正在尋找抽象語法來查看它作爲arg所用的內容以及返回的數據類型。 希望我可以選擇兩個提交的答案是正確的。 –