python os.scandir将apple dmg,app,bundle显示为目录

时间:2017-11-08 23:16:20

标签: python macos bundle hidden scandir

我正在为并行文件校验和编写程序。因为python os.scandir()显示Apple DMG,应用程序作为目录,所以我坚持使用子目录计数。

到目前为止,我已经获得了以下代码。

import os, sys

### variables
PATH_SRC = os.getcwd()
EXCLUDE_FILES = ('__', '.')
EXCLUDE_DIRS = ('__', '.', '*.app', '*.dmg')
LIST_FILES = []
LIST_DIRS = []

COUNT_DIRS = 0
COUNT_FILES = 0`

with os.scandir(PATH_SRC) as it:
  for entry in it:
    if not entry.name.startswith(EXCLUDE_FILES):
      if entry.is_dir() and entry.name.startswith(EXCLUDE_DIRS):
        LIST_DIRS.append(entry.path)
        COUNT_DIRS = COUNT_DIRS + 1
      elif entry.is_file():
        LIST_FILES.append(entry.path)
        COUNT_FILES = COUNT_FILES + 1

print ('Source path contains ', COUNT_DIRS, ' subdirectories.')
print ('Source path contains ', COUNT_FILES, ' file(s).')


print (LIST_DIRS)
print (LIST_FILES)

源路径包含10个子目录,但上面提到的代码显示16,因为有一些应用程序,dmg并且它们显示为目录。

我需要以递归方式列出源路径中的文件,并排除隐藏和捆绑(dmg,app)。统计它们......它将被用作决定是否使用并行化。我通常从闪存卡复制照片,但有时我会在我的文件服务器上复制整个尝试。

背后的想法是......计算源路径中的所有文件...例如,如果有超过10个文件...运行文件的并行校验和... SSD可以同时处理大约4个文件使CPU饱和。我的工作站有2个CPU,每个CPU有4个核心......但是会有SSD瓶颈。

此外,RAID 10文件服务器可以同时处理大约2-3个文件。

我是Python的新手,所以不要对复杂的代码感到惊讶。

AlGORITM: 1.检查总共有多少文件并决定MP(多处理或多线程)

  1. 递归检查源路径,排除隐藏文件和包...然后对它们运行校验和sha512

  2. 每个dir / subdir都必须有自己的校验和文件。我不希望源路径中有一个大的校验和文件

  3. 我希望你理解。请提出任何建议吗?

    此致

    Hajes

1 个答案:

答案 0 :(得分:0)

"应用" (" *。app ")确实是目录。

Take a look at Mac OS command ls

  

ls - 列出目录内容

     

可以使用以下选项:

     

...

-F      Display a slash (`/') immediately after each pathname that is a directory, an asterisk (`*')
         after each that is executable, an at sign (`@') after each symbolic link, an equals sign (`=')
         after each socket, a percent sign (`%') after each whiteout, and a vertical bar (`|') after
         each that is a FIFO
 -G      Enable colorized output.  This option is equivalent to defining CLICOLOR in the environment.
         (See below.)
     

...

User/Library目录的结果:

User/Library

Dirs被着色并附加/

Applications目录的结果:

Applications

所有应用*.app都是dirs。

其他文件的结果:

### And results for other files

*.dmg*.zip由于某种原因被标记为可执行文件。