943,677 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 38153
  • Python RSS
May 30th, 2008
0

Working with files in os.walk

Expand Post »
I am still reading the learing python o'reilly book and not sure the best way to approch my problem.

Given c:\dir1\dir2\dir3.

I want to zip all files in dir3 if those files are older than 30 days using 1 zip file (ie. dir_3_files.zip). If all files in dir3 are older 30 days, I want to zip the directory (ie. dir3.zip). I want to recursively keep doing this until I reach the top.

This is what I have so far, but I don't know where to interrupt os.walk to work with files in a directory. I hope this makes sense.

Python Syntax (Toggle Plain Text)
  1. import os, os.path, stat, time
  2. from datetime import date, timedelta
  3.  
  4. dirsNotUsed = []
  5.  
  6. def getdirs(basedir, age):
  7. for root, dirs, files in os.walk(basedir):
  8. basedate, lastused = datecheck(root, age)
  9. if lastused < basedate: #Gets files older than (age) days
  10. dirsNotUsed.append(root)
  11.  
  12. def datecheck(root, age):
  13. basedate = date.today() - timedelta(days=age)
  14. used = os.stat(root).st_mtime # st_mtime=modified, st_atime=accessed
  15. year, day, month = time.localtime(used)[:3]
  16. lastused = date(year, day, month)
  17. return basedate, lastused
  18.  
  19. def archive():
  20. pass
  21.  
  22. def main():
  23. basedir = raw_input('Choose directory to scan: ')
  24. age = raw_input('Only scan files older than... (days): ')
  25. getdirs(basedir, int(age))
  26.  
  27. if __name__ == '__main__':
  28. main()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ccandillo is offline Offline
16 posts
since May 2008
May 30th, 2008
0

Re: Working with files in os.walk

This will traverse all of the subdirectories and do what I think you want it to do. I have added some print statements which is probably enough by itself to answer your question as is shows what the root, dirs, and files contain. If you want to limit it to 3 levels, then you will want to store root and the first 2 dirs and pass them as basedir and then just use the files for that particular directory. If you are on a Linux system, pipe the output to a file if there are a lot of files and dirs. It will be easier to read. There are other ways of doing this using an os.path.walk() callback but I assume you want to continue down this road as you are following the book.
Python Syntax (Toggle Plain Text)
  1. import os, os.path, stat, time
  2. from datetime import date, timedelta
  3.  
  4. dirsNotUsed = []
  5. def getdirs(basedir, age):
  6. for root, dirs, files in os.walk(basedir):
  7. print "root =", root
  8. print "dirs =", dirs
  9. print "files =", files
  10.  
  11. found = 1
  12. for file in files:
  13. found_file = datecheck(root, file, age)
  14. if not found_file : #At least one file is not old enough
  15. found = 0
  16.  
  17. """ or backup all of the files that are old enough
  18. if found_file:
  19. backup_list.append(os.path.join(root, file))
  20. """
  21.  
  22. if found:
  23. archive(root, files)
  24.  
  25. def datecheck(root, file, age):
  26. basedate = date.today() - timedelta(days=age)
  27. fname = os.path.join(root, file)
  28. used = os.stat(fname).st_mtime # st_mtime=modified, st_atime=accessed
  29. year, day, month = time.localtime(used)[:3]
  30. lastused = date(year, day, month)
  31. if lastused < basedate: #Gets files older than (age) days
  32. return 1
  33. return 0 # Not old enough
  34.  
  35. def archive(root, files):
  36. for file in files:
  37. fname=os.path.join(root, file)
  38. print "archiving", fname
  39.  
  40. if __name__ == '__main__':
  41. basedir = raw_input('Choose directory to scan: ')
  42. age = raw_input('Only scan files older than... (days): ')
  43. getdirs(basedir, int(age))
Last edited by woooee; May 30th, 2008 at 5:29 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Jun 2nd, 2008
0

Re: Working with files in os.walk

For my first script, I thought I didn't do to bad. This makes much more sense to me now. By simply adding the extra print statements, found flag and the returns from the datecheck funtion really helped!

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ccandillo is offline Offline
16 posts
since May 2008
Jun 2nd, 2008
0

Re: Working with files in os.walk

Quote ...
By simply adding the extra print statements
That solves a lot of problems. You can also use os.path.walk with a callback.
Python Syntax (Toggle Plain Text)
  1. def processDirectory ( args, dirname, filenames ):
  2. print 'Directory',dirname
  3. for filename in filenames:
  4. print ' File',filename
  5.  
  6. top_level_dir = "/usr/local"
  7. os.path.walk(top_level_dir, processDirectory, None )
  8.  
  9. ##os.path.walk() works with a callback: processDirectory() will be
  10. ##called for each directory encountered.
Last edited by woooee; Jun 2nd, 2008 at 7:14 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Beginner Needs Help PYTHON student
Next Thread in Python Forum Timeline: problems with pygame





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC