首页 > 解决方案 > 使用网络路径检索 os.listdir

问题描述

我正在尝试获取用户输入以获取计算机名称,然后使用 os.listdir 检索该目录中的文件夹 - 特别是用户文件夹 (C:\users).. 我编写了以下代码并且似乎没有拉任何结果。运行代码“进程以退出代码 0 完成”时没有收到错误

import os
def listdirectory():
    computername = input("What is the computer name? ")
    completepath = r"\\" + computername + r"\C$\users"
    os.listdir(completepath)


listdirectory()

我正在尝试找出 os.listdir 是否可以以这种方式使用,如果不是我应该以哪种方式来写这个。

标签: pythonpython-3.x

解决方案


确保您正在对该输出进行操作!

import os
from typing import List

def listdirectory() -> List[str]:
    computername = input("What is the computer name? ")
    completepath = fr"\\{computername}\C$\Users"
    return os.listdir(completepath)

print(listdirectory())

为澄清起见,您的示例代码没有return来自函数的语句,因此None隐式返回。没有return和 没有print,您将看不到输出。此行为可能与 REPL 不同。


推荐阅读