繁体   English   中英

Python将*。*附加到字符串

[英]Python appending *.* to string

为什么

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

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

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

尝试不使用charList.txt

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

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

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.

您已为其指定了文件的路径,而不是目录的路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM