Python 3 - os.startfile() - WindowsError: [Error 2] The system cannot find the file specified: 'file_name'

Python 3 - os.startfile() - WindowsError: [Error 2] The system cannot find the file specified: 'file_name'

我正在尝试编写一个代码来搜索目录中的 mp3 文件并播放它。但是我收到了这个错误。代码是

import os

file_name = raw_input("File Name: ") #file to be searched
#cur_dir = raw_input("Search Directory: ") # Dir from where search starts 
can be replaced with any path

cur_dir = os.getcwd()

while True:
    file_list = os.listdir(cur_dir)
    parent_dir = os.path.dirname(cur_dir)
    if file_name in file_list:
        print ("File Exists in: "), cur_dir
        #
        os.startfile('file_name')
        #
        break
    else:
        if cur_dir == parent_dir: #if dir is root dir
            print ("File not found")
            break
        else:
            cur_dir = parent_dir

我得到的错误是

>>> 
File Name: Kalimba.mp3
File Exists in:  C:\Users\MrittikaMukut\Desktop\test

Traceback (most recent call last):
  File "C:\Users\MrittikaMukut\Desktop\test\search-2.py", line 16, in 
<module>
    os.startfile('file_name')
WindowsError: [Error 2] The system cannot find the file specified: 
'file_name'
>>>

但是当我 运行 一个类似于此的代码从一个目录中随机挑选一个 mp3 文件并播放它时,它工作正常。这是它的代码。

import random,os,sys

folder=os.listdir(os.getcwd())
file=random.choice(folder)
ext3=['.mp3']
print('First random pick: '+file)

while file[-4:] not in ext3 :
    print('Not an MP3 file  : '+file)
    file=random.choice(folder)
else:
    os.startfile(file)
    print('Song name: '+file)

sys.exit()

##os.startfile(random.choice(folder))

我很困惑,需要一些帮助。提前谢谢你。

删除 'file_name' 周围的引号。像下面这样使用它,

os.startfile(file_name)