我正在尝试获取用户输入以获取计算机名称,然后使用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 不同。