【问题标题】:How to list files in directory and subdirectory based on filesize greater than 10kb using python如何使用python根据大于10kb的文件大小列出目录和子目录中的文件
【发布时间】:2014-07-16 04:29:42
【问题描述】:

我经历了以下场景

  1. 我们可以列出目录中的文件
  2. 我们可以列出哪些文件及其文件大小

但现在我只需要列出文件,只要它们大于用户提供的输入的 X KB。

请提供一些合适的例子

这是我的代码

import os 
for path, dirs, files in os.walk("PathToDir" ): 
    for f in files: 
        size=os.path.getsize( os.path.join( path, f ) 
        print path, f, size

【问题讨论】:

  • 发布您尝试过但不起作用的代码。
  • 欢迎来到 Stack Overflow!看起来您希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的编码人员编写代码,但他们通常只有在发布者已经尝试自己解决问题时才会提供帮助。展示这项工作的一个好方法是包含您迄今为止编写的代码、示例输入(如果有的话)、预期输出和您实际获得的输出(控制台输出、堆栈跟踪、编译器错误 - 不管是什么适用的)。您提供的详细信息越多,您可能收到的答案就越多。
  • 嗯,感谢您的回复 import os for path, dirs, files in os.walk("PathToDir" ): for f in files: size=os.path.getsize( os.path.join( path, f ) 打印路径, f, 大小
  • 问题中包含代码
  • print 之前添加一个if size > 10000: 怎么样?

标签: python python-2.7


【解决方案1】:
limit = raw_input('Enter a file size: ')
if int(limit) > 0:
    import os 
    for path, dirs, files in os.walk("PathToDir" ): 
        for f in files: 
            size=os.path.getsize( os.path.join( path, f ) 
            if size > limit :
                print path, f, size

【讨论】:

    【解决方案2】:

    这是一个示例,说明如何“遍历”目录中的文件,然后打印出符合文件大小标准的文件:

    注意:如何“走路”在这里找到:

    concatenate the directory and file name

    # Task: List only files that are greater than some X KB with the input given by the user.
    
    
    import os
    
    # The directory that we are interested in
    myPath = "/users/george/documents/"
    
    # The min size of the file in Bytes
    mySize = '10000'
    
    # All the file paths will be stored in this list
    filesList= []
    
    for path, subdirs, files in os.walk(myPath):
        for name in files:
            filesList.append(os.path.join(path, name))
    
    for i in filesList:
        # Getting the size in a variable
        fileSize = os.path.getsize(str(i))
    
        # Print the files that meet the condition
        if int(fileSize) >= int(mySize):
            print "The File: " + str(i) + " is: " + str(fileSize) + " Bytes"
    

    【讨论】:

      【解决方案3】:

      我使用pathlib 模块实现了它。我在Windows 上运行Python 3.7.6

      代码如下:

      
      import os 
      from pathlib import Path
      
      dir_path = Path('//?/D:/TEST_DIRECTORY')
      
      # IMP_NOTE: If the path is 265 characters long, which exceeds the classic MAX_PATH - 1 (259) character
      # limit for DOS paths. Use an extended (verbatim) path such as "\\\\?\\C:\\" in order 
      # to access the full length that's supported by the filesystem -- about 32,760 characters. 
      # Alternatively, use Windows 10 with Python 3.6+ and enable long DOS paths in the registry.
      
      # pathlib normalizes Windows paths to use backslash, so we can use
      # Path('//?/D:/') without having to worry about escaping backslashes.
      
      
      F_LIST = list(x for x in dir_path.rglob('*.*') if x.is_file() and os.path.getsize(x) >= 10000)
      
      for f in F_LIST:
          print(f.parts[-1] + " ===> " + "Size = " + str(format(os.path.getsize(f), ',d')) + "\n")
      
      # path.parts ==> Provides a tuple giving access to the path’s various components
      # (Ref.: pathlib documentation)
      
      

      希望这会有所帮助! :-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-28
        • 2011-02-23
        • 2019-07-06
        • 2017-12-04
        • 1970-01-01
        • 1970-01-01
        • 2014-10-16
        • 2021-01-23
        相关资源
        最近更新 更多