#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0

    Red face File Size & Delete Files in Python


    I have 10 txt file in a directory.

    I need to search for file whoes size is < 10 k and delete those files.

    Please let me know the python code to do this.

    Thank you
    Shah
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Well I don't excatly know how to check the size but to delete the files:

    Code:
    import os
    
    os.remove(os.getcwd()+"\what ever file.py")
    Last edited by monkeyman23555; May 2nd, 2005 at 11:00 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Originally Posted by monkeyman23555
    Well I don't excatly know how to check the size but to delete the files:

    Code:
    import os
    
    os.remove(os.getcwd()+"\what ever file.py")
    Hey thanz, but i need to delete txt file based on file size.
    Thanz
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    hi again

    I guess this is a thing that might work in bytes or bites?
    I don't know I get confused???
    Code:
    >>> File = open("README.txt","r")
    >>> count = 0
    >>> for Char in File.read():
    	count += 1
    
    	
    >>> count
    49424
    It was teh README.txt file in teh home directory of python.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Originally Posted by shaharpan
    Please let me know the python code to do this.

    This is pretty roud. We are not here to do your coding for oyu. We are here to help guide you in the right direction withy our code and it is your job to apply it.

    But, you should look up the os module. Particuly the os.stat() and os.remove(). It should take about 15 lines of code to do what you want. If you need more help let us know.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Ya take it as serious as Cyberhigh

    Cool down man it is his own fault if he lets us do the coding since then he does not learn anything
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Originally Posted by monkeyman23555
    Ya take it as serious as Cyberhigh

    Cool down man it is his own fault if he lets us do the coding since then he does not learn anything
    Thank you guys,

    I know its not the rite way, but i needed this urgently,

    or i would have looked up the web myself.

    Thanz

    Comments on this post

    • monkeyman23555 disagrees : Thats really mean "or I would have to look it up myself."
    • sf2k disagrees : Hauuuper..."What is thy bidding o master?"
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    PHP Code:
    os.stat(FileName)[stat.ST_SIZE
    detailed
    PHP Code:
    import os
    import stat
    Directory 
    "."
    AllFiles os.listdir(Directory)
    for 
    File in AllFiles:
          
    FileName os.path.join(Directory,File)
          
    StatObject os.stat(FileName)
          
    FileSize StatObject[stat.ST_SIZE]
          if 
    FileSize<10*(2**10):
                
    os.remove(FileName
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Here's another example that should do what you want; if not then it shouldn't be hard to tweak it to do what you want.

    Code:
    #!/usr/bin/env python
    
    import os
    
    #Defines some default values for use in the program; the default path and the
    #default size (in KB).
    defaultPath = 'test'
    defaultSize = 4
    
    for eachName in os.listdir(defaultPath):
        eachName = os.path.join(defaultPath, eachName)
        if os.path.isfile(eachName) and \
                os.stat(eachName)[6] > defaultSize * 1024:
            try:
                print 'Removing', eachName
                os.remove(eachName)
            except OSError:
                print 'Cant remove', eachName
    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo