Khadafi995

Diff between file inside two specified folder

Feb 22nd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import glob
  6. import optparse
  7. import sys
  8. import pprint
  9.  
  10.  
  11. def main():
  12.     # Get the terminal option
  13.     parser = optparse.OptionParser(
  14.         "Using : python compare_filelist.py -a <dir> -b <dir>")
  15.     parser.add_option('-a',
  16.                       dest='dname1',
  17.                       type='string',
  18.                       help='specify folder')
  19.     parser.add_option('-b',
  20.                       dest='dname2',
  21.                       type='string',
  22.                       help='specify folder')
  23.     (options, args) = parser.parse_args()
  24.  
  25.     if (options.dname1 is None):
  26.         print(parser.usage)
  27.         sys.exit()
  28.     elif (options.dname2 is None):
  29.         print(parser.usage)
  30.         sys.exit()
  31.     else:
  32.         dname1 = options.dname1
  33.         dname2 = options.dname2
  34.  
  35.     # Get the file list of the given extension in the specified folder
  36.     list1 = glob.glob(os.path.join(dname1, '*.*'))
  37.     list2 = glob.glob(os.path.join(dname2, '*.*'))
  38.  
  39.     l11 = [os.path.basename(x) for x in list1]
  40.     l22 = [os.path.basename(y) for y in list2]
  41.  
  42.    
  43.     # Get the diff between of the list
  44.     list_file_manquants = [f2 for f2 in l22 if f2 not in l11]
  45.     l = str()
  46.  
  47.     # Return only the file name without the file path
  48.     for file in list_file_manquants:
  49.         try:
  50.             path, filename = os.path.split(file)
  51.             l += filename + '\n'
  52.         except:
  53.             pass
  54.  
  55.     print(l)
  56.  
  57.     # Write the file result into a file inside the second specified folder
  58.     with open(os.path.join(dname2, 'fichiers_manquants.txt'), 'w', encoding='utf8') as f:
  59.         f.write(l)
  60.  
  61. if __name__ == '__main__':
  62.     main()
Advertisement
Add Comment
Please, Sign In to add comment