1

I have gone through following scenarios

  1. which we can list files in a directory
  2. Which we can list files with their file sizes

But now I need to list only the files just they are greater than some X KB with the input given by the user.

Please help with some suitable examples

Here is my code

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
M4rtini
  • 13,186
  • 4
  • 35
  • 42
Nag Venkat
  • 41
  • 1
  • 5
  • Post the code that you tried that didn't work. – Burhan Khalid May 27 '14 at 09:59
  • 5
    Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Martijn Pieters May 27 '14 at 10:01
  • Well Thanks for the reply 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 – Nag Venkat May 27 '14 at 10:04
  • 1
    Include code in the __question__ – sshashank124 May 27 '14 at 10:09
  • I am just using to print all the files with specific path its name and its size how to print only the path files and size by the files greater than 10 Kb – Nag Venkat May 27 '14 at 10:09
  • 4
    How about adding an `if size > 10000:` before the `print`? – Alfe May 27 '14 at 10:11
  • So what's the problem? getting the input from the user, or comparing that input with the file sizes? – M4rtini May 27 '14 at 10:13
  • No firstly i need to just list the files based on the filesize with the help of a variable . Later i would add that as a input to user and then list the files with greater size – Nag Venkat May 27 '14 at 10:17
  • well except for a minor syntax error, you have the filesize there. And i assume you know how to compare that with a variable. – M4rtini May 27 '14 at 10:19
  • Thanks for the replies done with it forget about the if syntax Thanks #Alfe for your Valuable reply – Nag Venkat May 27 '14 at 10:28

3 Answers3

4

Here's an example of how to go 'walk' through the files in a directory, and then printing out the ones that meet a file size criterion:

Note: How to 'walk' was found here:

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"
Community
  • 1
  • 1
GeorgeZ
  • 51
  • 5
1

I achieved it using the pathlib module. I am running Python 3.7.6 on Windows.

Here's the code:


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)

Hope this helps! :-)

Amar
  • 111
  • 3
0
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
Louis
  • 2,854
  • 2
  • 19
  • 24