Working with files in os.walk

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 11
Reputation: ccandillo is an unknown quantity at this point 
Solved Threads: 0
ccandillo ccandillo is offline Offline
Newbie Poster

Working with files in os.walk

 
0
  #1
May 30th, 2008
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.

  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()
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,074
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Working with files in os.walk

 
0
  #2
May 30th, 2008
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: ccandillo is an unknown quantity at this point 
Solved Threads: 0
ccandillo ccandillo is offline Offline
Newbie Poster

Re: Working with files in os.walk

 
0
  #3
Jun 2nd, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,074
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Working with files in os.walk

 
0
  #4
Jun 2nd, 2008
By simply adding the extra print statements
That solves a lot of problems. You can also use os.path.walk with a callback.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 13498 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC