কম্পিউটার টিউটোরিয়াল

পাইথনে ফাইল তুলনা করার পদ্ধতি

সমস্যা

পাইথনে ফাইলগুলো তুলনা করতে হবে।

সমাধান

পাইথনের filecmp মডিউল ফাইল এবং ডিরেক্টরি তুলনা করতে ব্যবহার করা যায়।

১. cmp(file1, file2[, shallow])

দুটি ফাইল তুলনা করে সেম হলে True, নয় তবে False ফেরত দেয়। ডিফল্টে os.stat() থেকে একই অ্যাট্রিবিউট থাকলে ফাইলগুলো সমান মाना হয়। shallow=True হলে শুধু স্ট্যাট সিগনেচার মিলিয়ে ফেলে।

২. cmpfiles(dir1, dir2, common[, shallow])

দুটি ডিরেক্টরির common লিস্টের ফাইল কন্টেন্ট তুলনা করে। তিনটি লিস্ট ফেরত দেয়: match, mismatch, errors।

  • match - উভয় ডিরেক্টরিতে একই ফাইল।
  • mismatch - মিলে না এমন ফাইল।
  • errors - যেগুলো তুলনা করা যায়নি।

৩. dircmp(dir1, dir2 [, ignore[, hide]])

ডিরেক্টরি তুলনা অবজেক্ট তৈরি করে যা বিভিন্ন তুলনা অপারেশন করতে পারে।

  • ignore - ইগনোর করার ফাইলনেম লিস্ট, ডিফল্ট ['RCS','CVS','tags']।
  • hide - হাইড করার ফাইলনেম লিস্ট, ডিফল্ট [os.curdir, os.pardir]।

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)
    return

if __name__ == '__main__':
    os.chdir(os.getcwd())
    makedirectory('example')
    makedirectory('example/dir1/common_dir')
    makedirectory('example/dir2/common_dir')

filecmp উদাহরণ

shallow আর্গুমেন্ট বলে দেয় কন্টেন্ট চেক করবে কিনা। ডিফল্ট শ্যালো কমপারিজন (মেটাডেটা ব্যবহার করে)। shallow=False হলে কন্টেন্ট সবসময় তুলনা হয়।

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(f"output \n *** Printing detaile report: \n ")
print(dc.report())
print(f"\n")
print(dc.report_full_closure())
  1. পাইথনে ডিরেক্টরির ফাইল লিস্ট করা: ধাপে ধাপে গাইড

  2. পাইথনে PDF ফাইল ক্র্যাক (পাসওয়ার্ড খোলা) করা

  3. পাইথনে ফাইলগুলো নতুন ডিরেক্টরিতে কপি করার পদ্ধতি

  4. পাইথনে cd কমান্ড চালানোর পদ্ধতি