简体   繁体   English

Python将*。*附加到字符串

[英]Python appending *.* to string

Why does 为什么

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

give this error: 给出这个错误:

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

I thought it was an issue with the string encoding, but apparently not? 我以为这是字符串编码的问题,但显然不是吗?

Here: 'C:\\Users\\tom\\Desktop\\PythonScripts\\charList.txt' . 此处: 'C:\\Users\\tom\\Desktop\\PythonScripts\\charList.txt' That's a file name , not a directory name . 那是一个文件名 ,而不是目录名

Try without charList.txt : 尝试不使用charList.txt

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

Any particular reason why you're appending the filename to a listdir operation? 要将文件名附加到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.

You've given it a path to a file, not a directory. 您已为其指定了文件的路径,而不是目录的路径。

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

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