Python将*。*附加到字符串

时间:2013-12-08 23:29:28

标签: python operating-system listdir

为什么

>>> 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/*.*'

我认为这是字符串编码的问题,但显然不是吗?

2 个答案:

答案 0 :(得分:5)

此处:'C:\Users\tom\Desktop\PythonScripts\charList.txt'。这是文件名,而不是目录名

尝试不使用charList.txt

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

您将文件名附加到listdir操作的任何特定原因?

答案 1 :(得分: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.

您已经为它提供了文件的路径,而不是目录。