【问题标题】:Python appending *.* to stringPython 将 *.* 附加到字符串
【发布时间】:2013-12-08 23:29:28
【问题描述】:

为什么

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

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

【问题讨论】:

    标签: python operating-system listdir


    【解决方案1】:

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

    尝试不使用charList.txt

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

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

    【讨论】:

    • 这就是问题所在。看来我得去喝杯咖啡了。我从来没有想过,但现在完全有道理!你先生真棒!非常感谢!!显然我必须等待 9 分钟才能选择正确答案...
    • 我不能在工作中“休息”。但我可以做其他事情,比如提高我的 Python 技能。 >:)
    【解决方案2】:
    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.
    

    你给了它一个文件的路径,而不是一个目录。

    【讨论】:

    • 很好的解释。我一直在寻找抽象语法来查看它作为 arg 的含义以及返回的数据类型。希望我可以选择两个提交的答案都是正确的。
    猜你喜欢
    • 2019-02-13
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2012-10-04
    • 2020-02-04
    相关资源
    最近更新 更多