所以,我試圖讓自己成爲一個Python腳本,通過選定的音樂文件夾並告訴用戶特定專輯是否沒有專輯封面。它基本上通過所有的文件並檢查if file[-4:] in (".jpg",".bmp",".png")
,如果是的話,它找到了一個圖片文件。只是要清楚,我的文件夾的結構是:python os.listdir()顯示受保護的文件
- 音樂文件夾
- 狂賣
- Humbug的(2009)
- 吮吸它,見(2011)
- 嗎啡
- 治癒疼痛(1993)
- 狂賣
..等等。我正在測試腳本以查找Arctic Monkeys目錄中是否缺少封面,並且我的腳本通過「Humbug(2009)」文件夾並找到AlbumArtSmall.jpg which doesn't show up in the command prompt,所以我嘗試「顯示隱藏的文件/文件夾」並仍然沒有。但是,the files show up once I uncheck "Hide protected operating system files",所以這有點奇怪。
我的問題是 - 我該如何告訴Python跳過搜索隱藏/受保護的文件? 我檢出了How to ignore hidden files using os.listdir()?,但我在那裏找到的解決方案只適用於以「。」開頭的文件,而這不是我所需要的。
乾杯!
編輯 - 所以這裏的代碼:
import os
def findCover(path, band, album):
print os.path.join(path, band, album)
coverFound = False
for mFile in os.listdir(os.path.join(path, band, album)):
if mFile[-4:] in (".jpg",".bmp",".png"):
print "Cover file found - %s." % mFile
coverFound = True
return coverFound
musicFolder = "E:\Music" #for example
noCovers = []
for band in os.listdir(musicFolder): #iterate over bands inside the music folder
if band[0:] == "Arctic Monkeys": #only Arctic Monkeys
print band
bandFolder = os.path.join(musicFolder, band)
for album in os.listdir(bandFolder):
if os.path.isdir(os.path.join(bandFolder,album)):
if findCover(musicFolder, band, album): #if cover found
pass #do nothing
else:
print "Cover not found"
noCovers.append(band+" - "+album) #append to list
else: #if bandFolder is not actually a folder
pass
print ""
一個正確的答案將取決於你如何穿過你的目錄,但是,你有沒有想過用[os.stat]檢查文件模式(http://docs.python.org/library /os.html#os.stat)? –
我已經添加了代碼,希望有所幫助。無論如何,我嘗試了'os.stat(「Folder.jpg」)',這是我得到的: 'nt.stat_result(st_mode = 33206,st_ino = 0L,st_dev = 0,st_nlink = 0,st_uid = 0, st_gid = 0,st_size = 34820L,st_atime = 1315277420L,st_mtime = 1259528972L,st_ctime = 1259525728L)'。我不確定哪些值告訴我該文件是「受保護」或「隱藏」的。嗯。 –
對可見的.mp3文件執行os.stat()會返回幾乎相同的值:'nt.stat_result(st_mode = 33206,st_ino = 0L,st_dev = 0,st_nlink = 0,st_uid = 0,st_gid = 0,st_size = 8379620L,st_atime = 1315277422L,st_mtime = 1259529036L,st_ctime = 1250607460L)'。 –