【问题标题】:`os.listdir` throws Error2 for `~` [duplicate]`os.listdir`为`~`抛出Error2 [重复]
【发布时间】:2021-03-07 06:17:43
【问题描述】:

python 版本:3.8 操作系统:Manjaro KDE

当我打电话给os.listdir('/home/user_name') 时,它工作正常。

但是,当我调用os.listdir('~') 时,它抛出了FileNotFoundError: [Errno 2] No such file or directory: '~'

为什么os.listdir 无法理解~

【问题讨论】:

    标签: python-3.8


    【解决方案1】:
    【解决方案2】:

    你可以这样使用它:

    p = os.path.expanduser('~')
    os.listdir(p)
    

    您可以参考这些链接:

    Python's os.makedirs doesn't understand “~” in my path

    From documentation

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2013-12-30
      • 2020-02-07
      • 2023-03-14
      相关资源
      最近更新 更多