ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
remove_backups.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 
5 import argparse
6 import os
7 
8 def FullPath(path):
9  return os.path.abspath(os.path.expanduser(path))
10 
11 def RemoveBackups(file_dir):
12  full_path = FullPath(file_dir)
13  if os.path.isdir(full_path):
14  contents = []
15  try:
16  contents = os.listdir(full_path)
17  except OSError as err:
18  print("Cannot access directory "+full_path+" (OSError "+str(err.errno)+": "+err.strerror+")")
19  for fd in contents:
20  RemoveBackups(os.path.join(full_path, fd))
21  elif os.path.isfile(full_path):
22  base_name = os.path.basename(full_path)
23  if base_name.endswith('~') or (base_name.startswith('#') and base_name.endswith('#')):
24  try:
25  os.remove(full_path)
26  except OSError as err:
27  print("Cannot remove file "+full_path+" (OSError "+str(err.errno)+": "+err.strerror+")")
28 
29 if __name__ == "__main__":
30  parser = argparse.ArgumentParser(description="Removes all emacs backup files from given directories",
31  formatter_class=argparse.ArgumentDefaultsHelpFormatter)
32  parser.add_argument("input_dir", nargs="*", default=["."], metavar="INPUT_DIR",
33  help="Directories from which to remove backup files")
34  args = parser.parse_args()
35 
36  for d in args.input_dir:
37  RemoveBackups(d)
def FullPath(path)
def RemoveBackups(file_dir)