我有一個python文件來打印另一個文件的完整路徑,該文件可以在其他目錄之一中。我的文件夾結構如下:查找未知目錄中的文件的路徑
D:\Main\Script\Myscript.py
D:\Main\Target\A\cow.txt
D:\Main\Target\B\dog.txt,p1.txt
D:\Main\Target\c\cat.txt
D:\Main\Target\D\Q1.txt
當我給出文件名時,我想打印該文件的完整路徑。例如,如果我給「Q1.txt」,它應該打印D:\Main\Target\D\Q1.txt
。我嘗試了以下內容,讓我的輸入文件爲Myfile1
。
Scriptloc=os.path.join(os.path.dirname(__file__))
if (os.path.exist(Scriptloc+".."+os.sep+".."+os.sep+A+Myfile1)):
print "Filepath is "+Scriptloc+".."+os.sep+".."+os.sep+A+Myfile1
elif (os.path.exist(Scriptloc+".."+os.sep+".."+os.sep+A+Myfile1)):
print "Filepath is "+Scriptloc+".."+os.sep+".."+os.sep+B+Myfile1
elif (os.path.exist(Scriptloc+".."+os.sep+".."+os.sep+C+Myfile1)):
print "Filepath is "+Scriptloc+".."+os.sep+".."+os.sep+C+Myfile1
elif (os.path.exist(Scriptloc+".."+os.sep+".."+os.sep+D+Myfile1)):
print "Filepath is "+Scriptloc+".."+os.sep+".."+os.sep+C+Myfile1
else:
print "File not found"
有沒有其他簡單的方法來做到這一點?
如果有名爲'Q1.txt'幾個文件? – bereal
什麼是''''? –
'.. \'將給父目錄 – TVSuser1654136