我想制作一个程序,从用户那里获取文件名并显示文件的绝对路径

I want to make a program that takes file name from user and displays absolute path of the file

所以我基本上想做的是一个程序,它要求用户提供文件并显示程序的绝对路径。例如。如果用户输入 'Spotify' 那么我想在电脑上找到 spotify.exe 如果它存在并打印它的绝对路径。

我想为我正在工作的私人助理做这个。

我之前试过

os.system(input + '.exe') 但仅以 spotify 为例...它不是一个可以识别的内部文件...请帮忙。

你试过了吗

os.path.abspath(文件)

如果您正在使用 Linux,您可以使用 os.system('which spotify')

您可以使用 os.walk 获取文件的位置,然后使用 os.path.abspath 获取完整的绝对路径名。

相关回答: