Clean files/directory by time.
_________________________________
*Does not check permissions. python cleanFiles.py --directory=/home/win98/tmp/ --ageInDays=2
_________________________________________
1. #!/usr/bin/env python
2. import argparse
3. import os
4. import sys
5. from time import strptime, time
6.
7. def parse_arguments (args) :
8. argParse = argparse.ArgumentParser ( \
9. description= 'Deletes file in a directory older than a period in days' )
10.
11. argParse.add_argument ('--directory' \
12. , help = 'Target directory'
13. , required = True
14. , type = check_directory )
15.
16. argParse.add_argument ('--ageInDays' \
17. , help = 'Integer age in days'
18. , required = True
19. , type = int )
20.
21. return argParse.parse_args ()
22.
23. def unlink_files_by_days (targetDirectory, ageInDays ):
24. for checkFile in os.listdir (targetDirectory ):
25. filePath = os .path.join (targetDirectory, checkFile )
26. fileAgeInDays = get_file_age_in_days (filePath)
27. if ( fileAgeInDays >= ageInDays and os .path.isfile (filePath)== True) :
28. print 'Deleting {0} {1}' .format (filePath, fileAgeInDays )
29. os .unlink (filePath)
30.
31. def get_file_age_in_days (filePath):
32. return ( time() - os. stat(filePath) .st_mtime )/86400
33.
34. def check_directory (checkDirectory ) :
35. if os .path.isdir ( checkDirectory) :
36. return checkDirectory
37. print 'Invalid directory {0}' .format ( checkDirectory )
38. exit ()
39.
40. def main () :
41. validArgs = parse_arguments (sys .argv )
42. unlink_files_by_days ( validArgs.directory, validArgs.ageInDays )
43.
44. if __name__== '__main__' :
45. main ()
_________________________________________
Thanks For Visiting.....
Vivek kumar
_________________________________
*Does not check permissions. python cleanFiles.py --directory=/home/win98/tmp/ --ageInDays=2
_________________________________________
1. #!/usr/bin/env python
2. import argparse
3. import os
4. import sys
5. from time import strptime, time
6.
7. def parse_arguments (args) :
8. argParse = argparse.ArgumentParser ( \
9. description= 'Deletes file in a directory older than a period in days' )
10.
11. argParse.add_argument ('--directory' \
12. , help = 'Target directory'
13. , required = True
14. , type = check_directory )
15.
16. argParse.add_argument ('--ageInDays' \
17. , help = 'Integer age in days'
18. , required = True
19. , type = int )
20.
21. return argParse.parse_args ()
22.
23. def unlink_files_by_days (targetDirectory, ageInDays ):
24. for checkFile in os.listdir (targetDirectory ):
25. filePath = os .path.join (targetDirectory, checkFile )
26. fileAgeInDays = get_file_age_in_days (filePath)
27. if ( fileAgeInDays >= ageInDays and os .path.isfile (filePath)== True) :
28. print 'Deleting {0} {1}' .format (filePath, fileAgeInDays )
29. os .unlink (filePath)
30.
31. def get_file_age_in_days (filePath):
32. return ( time() - os. stat(filePath) .st_mtime )/86400
33.
34. def check_directory (checkDirectory ) :
35. if os .path.isdir ( checkDirectory) :
36. return checkDirectory
37. print 'Invalid directory {0}' .format ( checkDirectory )
38. exit ()
39.
40. def main () :
41. validArgs = parse_arguments (sys .argv )
42. unlink_files_by_days ( validArgs.directory, validArgs.ageInDays )
43.
44. if __name__== '__main__' :
45. main ()
_________________________________________
Thanks For Visiting.....
Vivek kumar
No comments:
Post a Comment