5 Replies - 217 Views - Last Post: 22 August 2014 - 01:08 PM Rate Topic: -----

#1 Sciptzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-August 14

Count files and size

Posted 22 August 2014 - 12:03 PM

Hey guys, I am new here and new to python so please bare with me if I make any mistakes. I am working on a simple program to count files in particular folders and return the total number of files and total size of the entire folder. I would like to also check against the files if they exceed a certain size. If it exceeds that size I would like to know which file. That is the part I am having difficulties with, I cannot seem to piece that together to get a return number of files that exceeded the limit. I have tried researching it with no luck. Any help or a point in the right direction would be appreciated. Here is what I got so far.

import os, stat, csv
from os import listdir
from os.path import isfile

user_profile = os.environ['USERPROFILE']
user_downloads = user_profile + "/Downloads"

files1 = sum([len(files) for r, d, files in os.walk(user_downloads)])
print
print 'Number of files in Downloads folder = ',files1
print
size1 = os.path.getsize(user_downloads)

a = csv.writer(open("MYFILE.csv", "a"))
a.writerow([user_profile, files1, size1,])

exit = raw_input('Press ENTER to exit')
print 'Closing....'



Is This A Good Question/Topic? 0
  • +

Replies To: Count files and size

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Count files and size

Posted 22 August 2014 - 12:30 PM

What is the problem with your code? What does it do or not do?

Please use a sensible title for your question, everyone could use "A little help with this code", it tells us nothing.

This post has been edited by andrewsw: 22 August 2014 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Count files and size

Posted 22 August 2014 - 12:36 PM

For what you are now trying to do I would remove the comprehension, and introduce a simple for-loop to iterate the files. Then you can break the requirements down into single steps.

This post has been edited by andrewsw: 22 August 2014 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sciptzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-August 14

Re: Count files and size

Posted 22 August 2014 - 12:40 PM

View Postandrewsw, on 22 August 2014 - 12:30 PM, said:

What is the problem with your code? What does it do or not do?

Please use a sensible title for your question, everyone could use "A little help with this code", it tells us nothing.


I stated in the initial post what it is I need help with...

View PostSciptzor, on 22 August 2014 - 12:03 PM, said:

I would like to also check against the files if they exceed a certain size. If it exceeds that size I would like to know which file. That is the part I am having difficulties with, I cannot seem to piece that together to get a return number of files that exceeded the limit.


I apologize for not making it clear enough. Allow me to elaborate. I am scanning the "Downloads" folder for the total number of files and total size of the folder in kb. I need a total count of how many files in the "Downloads" folder exceed 20kb. I do not know how to begin writing this into the code. I need to be steered in the right direction. Again, I apologize for not stating this initially.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Count files and size

Posted 22 August 2014 - 12:56 PM

I recommended that your first step, as I mentioned, is to not use the list comprehension; you need to use a for-loop to iterate the files, as you now need to do more with these files than just summing them.

Re-write your code without the comprehension, then you can start to break down the requirement into steps.
Was This Post Helpful? 0
  • +
  • -

#6 Sciptzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-August 14

Re: Count files and size

Posted 22 August 2014 - 01:08 PM

View Postandrewsw, on 22 August 2014 - 12:56 PM, said:

I recommended that your first step, as I mentioned, is to not use the list comprehension; you need to use a for-loop to iterate the files, as you now need to do more with these files than just summing them.

Re-write your code without the comprehension, then you can start to break down the requirement into steps.


Thank you for your insight! I will look into the for-loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1