python - 使用网络路径检索 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 是否可以以这种方式使用,如果不是我应该以哪种方式来写这个。
解决方案
确保您正在对该输出进行操作!
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 不同。
推荐阅读
- amp-html - (gtag) for AMP 在配置中未找到触发器。不会发送任何分析数据
- c# - 反序列化 XML 时绕过特定的父节点?
- reactjs - 如何在 MobX 中使用 @observable 装饰器定义的更改对象上添加侦听器以在 React 中使用
- javascript - 运行 minio 服务器时不会出现对 minio 浏览器的更改
- javascript - 关闭 ASP.NET 页面
- r - 循环中对数据帧的操作
- computer-vision - 转换 TensorFlow 模型时出现 OpenVino 模型优化器错误
- sql-server - 如何基于Select查询在sql server中添加新列
- python - 有没有办法在 FacetGrid seaborn 中指定子图标题?
- akeneo - Akeneo 导入在上传时返回 500