python os.listdir() 显示受保护的文件 [英] python os.listdir() shows protected files

查看:124
本文介绍了python os.listdir() 显示受保护的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图让自己成为一个 Python 脚本,它遍历选定的音乐文件夹并告诉用户特定专辑是否没有专辑封面.它基本上遍历所有文件并检查if file[-4:] in (".jpg",".bmp",".png"),如果为真,则找到一个图片文件.为了清楚起见,我的文件夹结构是:

So, I'm trying to make myself a Python script which goes through the selected Music folder and tells the user if specific album doesn't have an album cover. It basically goes through all the files and checks if file[-4:] in (".jpg",".bmp",".png"), if true, it found a picture file. Just to make it clear, the structure of my folders is:

  • 音乐文件夹
    • 北极猴
      • Humbug (2009)
      • 吮吸它,看看 (2011)
      • 治愈疼痛 (1993)

      .. 等等.我正在测试脚本以查找我的 Arctic Monkeys 目录中是否缺少封面,并且我的脚本通过Humbug (2009)"文件夹找到 AlbumArtSmall.jpg 它没有出现在命令提示符中 所以我尝试了显示隐藏的文件/文件夹",但仍然没有.但是,一旦我取消选中隐藏受保护的操作系统文件",这些文件就会出现,所以有点奇怪.

      .. and so on. I'm testing the script to find if there's a missing cover in my Arctic Monkeys directory, and my script goes through the "Humbug (2009)" folder and finds AlbumArtSmall.jpg which doesn't show up in the command prompt so I tried "Show hidden files/folders" and still nothing. However, the files show up once I uncheck "Hide protected operating system files", so that's kinda weird.

      我的问题是 - 我如何告诉 Python 跳过搜索隐藏/受保护的文件?我查看了 How to ignore hidden files using os.listdir()? 但我在那里找到的解决方案只适用于以."开头的文件,而这不是我需要的.

      My question is - how do I tell Python to skip searching the hidden/protected files? I checked out the How to ignore hidden files using os.listdir()? but the solution I found there only works for files starting with ".", and that's not what I need.

      干杯!

      编辑 - 这是代码:

      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 ""
      

      推荐答案

      您可以使用 pywin32 模块,并手动测试FILE_ATTRIBUTE_HIDDEN 或任意数量的属性

      You can use with the pywin32 module, and manually test for FILE_ATTRIBUTE_HIDDEN or any number of attributes

      FILE_ATTRIBUTE_ARCHIVE              = 32
      FILE_ATTRIBUTE_ATOMIC_WRITE         = 512
      FILE_ATTRIBUTE_COMPRESSED           = 2048
      FILE_ATTRIBUTE_DEVICE               = 64
      FILE_ATTRIBUTE_DIRECTORY            = 16
      FILE_ATTRIBUTE_ENCRYPTED            = 16384
      FILE_ATTRIBUTE_HIDDEN               = 2
      FILE_ATTRIBUTE_NORMAL               = 128
      FILE_ATTRIBUTE_NOT_CONTENT_INDEXED  = 8192
      FILE_ATTRIBUTE_OFFLINE              = 4096
      FILE_ATTRIBUTE_READONLY             = 1
      FILE_ATTRIBUTE_REPARSE_POINT        = 1024
      FILE_ATTRIBUTE_SPARSE_FILE          = 512
      FILE_ATTRIBUTE_SYSTEM               = 4
      FILE_ATTRIBUTE_TEMPORARY            = 256
      FILE_ATTRIBUTE_VIRTUAL              = 65536
      FILE_ATTRIBUTE_XACTION_WRITE        = 1024
      

      像这样:

      import win32api, win32con
      
      #test for a certain type of attribute
      attribute = win32api.GetFileAttributes(filepath)
      #The file attributes are bitflags, so you want to see if a given flag is 1.
      # (AKA if it can fit inside the binary number or not) 
      # 38 in binary is  100110 which means that 2, 4 and 32 are 'enabled', so we're checking for that
      ## Thanks to Nneoneo
      if attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM):
        raise Exception("hidden file") #or whatever
      
      #or alter them
      win32api.SetFileAttributes(filepath, win32con.FILE_ATTRIBUTE_NORMAL) #or FILE_ATTRIBUTE_HIDDEN
      

      更改文件后,查看文件夹,它将不再隐藏.

      After you alter a file, take a look in the folder, it won't be hidden anymore.

      此处和此处找到此信息: 在python中检查文件属性

      Found this information here and here: Checking file attributes in python

      或者,您可以尝试使用 os.stat 函数,其文档 此处 然后使用 stat 模块 以进一步了解您正在查看的内容.

      Alternatively, you can try to use the os.stat function, whose docs here and then use the stat module to further understand what you're looking at.

      找到了这些相关问题.(python) st_mode 的含义如何获取文件的权限掩码?

      Found these relevant questions. (python) meaning of st_mode and How can I get a file's permission mask?

      这篇关于python os.listdir() 显示受保护的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆