समस्या
आपको पायथन में फाइलों की तुलना करने की आवश्यकता है।
समाधान
पायथन में filecmp मॉड्यूल का उपयोग फाइलों और डायरेक्टरीज़ की तुलना के लिए किया जा सकता है।
1. cmp(file1, file2[, shallow])
filecmp फाइल्स file1 और file2 की तुलना करता है और समान होने पर True, अलग होने पर False लौटाता है। डिफ़ॉल्ट रूप से, जिन फाइलों के आइडेंटिकल एट्रिब्यूट्स होते हैं जैसा कि os.stat() द्वारा लौटाया जाता है, उन्हें समान माना जाता है। अगर shallow प्रदान नहीं किया गया है (या True है), तो जिन फाइलों का समान स्टेट सिग्नेचर है, उन्हें समान माना जाता है।
2. cmpfiles(dir1, dir2, common[, shallow])
दो डायरेक्टरीज़ dir1 और dir2 में निहित फाइलों की सामग्री की तुलना करता है जो लिस्ट common में हैं। cmpfiles तीन सूचियों वाला एक टपल लौटाता है - match, mismatch, errors फाइलनाम्स के।
match- दोनों डायरेक्टरीज़ में समान फाइलें सूचीबद्ध करता है।mismatch- मेल न खाने वाली फाइलें सूचीबद्ध करता है।errors- उन फाइलों को सूचीबद्ध करता है जिनकी किसी कारण से तुलना नहीं की जा सकी।
3. dircmp(dir1, dir2 [, ignore[, hide]])
एक डायरेक्टरी तुलना ऑब्जेक्ट बनाता है जिसका उपयोग डायरेक्टरीज़ dir1 और dir2 पर विभिन्न तुलना ऑपरेशन्स करने के लिए किया जा सकता है।
ignore- अनदेखा करने के लिए फाइलनाम्स की सूची, डिफ़ॉल्ट मान['RCS', 'CVS', 'tags']।hide- छिपाने के लिए फाइलनाम्स की सूची, डिफ़ॉल्ट[os.curdir, os.pardir](['.', '..']UNIX पर)।
filecmp.dircmp के इंस्टेंसेस निम्नलिखित मेथड्स इम्प्लीमेंट करते हैं जो sys.stdout पर विस्तृत रिपोर्ट प्रिंट करते हैं:
report(): दो डायरेक्टरीज़ के बीच तुलना प्रिंट करता है।report_partial_closure(): दो डायरेक्टरीज़ के साथ-साथ दोडायरेक्टरीज़ के तत्काल सबडायरेक्टरीज़ की तुलना प्रिंट करता है।report_full_closure(): दो डायरेक्टरीज़, उनके सभी सबडायरेक्टरीज़, उन सबडायरेक्टरीज़ के सभी सबडायरेक्टरीज़, और इसी तरह (यानी रिकर्सिवली) की तुलना प्रिंट करता है।
इसके अलावा कई उपयोगी एट्रिब्यूट्स भी हैं जैसे left_list, right_list, common, left_only, right_only, common_dirs, common_files, same_files, diff_files, funny_files, subdirs।
तुलना के लिए टेस्ट डेटा तैयार करना
import os
def makefile(filename, text=None):
with open(filename, 'w') as f:
f.write(text or filename)
return
def makedirectory(directory_name):
if not os.path.exists(directory_name):
os.mkdir(directory_name)
present_directory = os.getcwd()
os.chdir(directory_name)
os.mkdir('dir1')
os.mkdir('dir2')
os.mkdir('dir1/common_dir')
os.mkdir('dir2/common_dir')
os.mkdir('dir1/dir_only_in_dir1')
os.mkdir('dir2/dir_only_in_dir2')
makefile('dir1/file_only_in_dir1')
makefile('dir2/file_only_in_dir2')
makefile('dir1/common_file', 'Hello, Writing Same Content')
makefile('dir2/common_file', 'Hello, Writing Same Content')
makefile('dir1/not_the_same')
makefile('dir2/not_the_same')
makefile('dir1/file_in_dir1', 'This is a file in dir1')
os.mkdir('dir2/file_in_dir1')
os.chdir(present_directory)
filecmp उदाहरण
shallow आर्गुमेंट cmp() को बताता है कि फाइल की सामग्री को देखें, इसके मेटाडेटा के अलावा।
import filecmp
print('Output \n *** Common File :', end=' ')
print(filecmp.cmp('example/dir1/common_file', 'example/dir2/common_file'), end=' ')
print(filecmp.cmp('example/dir1/common_file', 'example/dir2/common_file', shallow=False))
print(' *** Different Files :', end=' ')
print(filecmp.cmp('example/dir1/not_the_same', 'example/dir2/not_the_same'), end=' ')
print(filecmp.cmp('example/dir1/not_the_same', 'example/dir2/not_the_same', shallow=False))
print(' *** Identical Files :', end=' ')
print(filecmp.cmp('example/dir1/file_only_in_dir1', 'example/dir1/file_only_in_dir1'), end=' ')
print(filecmp.cmp('example/dir1/file_only_in_dir1', 'example/dir1/file_only_in_dir1', shallow=False))
आउटपुट:
*** Common File : True True
*** Different Files : False False
*** Identical Files : True True
cmpfiles उदाहरण
import filecmp
import os
dir1_contents = set(os.listdir('example/dir1'))
dir2_contents = set(os.listdir('example/dir2'))
common = list(dir1_contents & dir2_contents)
common_files = [f for f in common if os.path.isfile(os.path.join('example/dir1', f))]
print(f' *** Common files are : {common_files}')
match, mismatch, errors = filecmp.cmpfiles(
'example/dir1', 'example/dir2', common_files,
)
print(f' *** Matched files are : {match}')
print(f' *** mismatch files are : {mismatch}')
print(f' *** errors files are : {errors}')
आउटपुट:
*** Common files are : ['file_in_dir1', 'not_the_same', 'common_file']
*** Matched files are : ['common_file']
*** mismatch files are : ['file_in_dir1', 'not_the_same']
*** errors files are : []
डायरेक्टरीज़ की तुलना
import filecmp
dc = filecmp.dircmp('example/dir1', 'example/dir2')
print("output \n *** Printing detailed report: \n")
print(dc.report())
print("\n")
print(dc.report_full_closure())