简体   繁体   中英

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' . That's a file name , not a directory name .

Try without charList.txt :

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

Any particular reason why you're appending the filename to a listdir operation?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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