কম্পিউটার

পাইথনের একটি ফোল্ডার থেকে একাধিক টেক্সট ফাইল কিভাবে পড়তে হয়? (Tkinter)


পাইথন ফাইল, বস্তু পরিচালনা করতে এবং বিভিন্ন অ্যাপ্লিকেশন তৈরি করতে সক্ষম। আমরা পাইথনের এক্সটেনশন এবং প্যাকেজগুলি সম্পূর্ণরূপে বৈশিষ্ট্যযুক্ত অ্যাপ্লিকেশনগুলি তৈরি এবং বিকাশ করতে ব্যবহার করতে পারি৷

ধরুন আপনি আপনার সিস্টেমের ফাইলগুলি নিয়ন্ত্রণ করতে চান; তারপর পাইথন একটি OS মডিউল প্রদান করে যেটিতে সিস্টেম-সক্ষম কার্যকারিতা রয়েছে যাতে আপনি অপারেটিং সিস্টেমের ফাইলগুলির সাথে ইন্টারঅ্যাক্ট করতে পারেন৷

আসুন দেখি কিভাবে আমরা পাইথনের OS মডিউল ব্যবহার করে একটি ফোল্ডার থেকে একাধিক টেক্সট ফাইল পড়তে পারি।

  • আপনার নোটবুকে OS মডিউল আমদানি করুন৷

  • আপনার সিস্টেমে পাঠ্য ফাইলগুলি যেখানে অবস্থিত সেখানে একটি পথ নির্ধারণ করুন।

  • ফাইলগুলির একটি তালিকা তৈরি করুন এবং তাদের সকলের সঠিক এক্সটেনশন আছে কিনা তা খুঁজে বের করতে পুনরাবৃত্তি করুন৷

  • মডিউলে সংজ্ঞায়িত ফাংশন ব্যবহার করে ফাইলগুলি পড়ুন।

উদাহরণ

# Import the required libraries
import os

# Define the location of the directory
path =r"C:/Users/Sairam/Documents/"

# Change the directory
os.chdir(path)

def read_files(file_path):
   with open(file_path, 'r') as file:
      print(file.read())

# Iterate over all the files in the directory
for file in os.listdir():
   if file.endswith('.txt'):
      # Create the filepath of particular file
      file_path =f"{path}/{file}"

read_files(file_path)

আউটপুট

Sample 1
========
Welcome to Tutorialspoint.

You are browsing the best resource for Online Education.

Sample 2
========
A distributed ledger is a type of data structure which resides across multiple computer devices, generally spread across locations or regions.

Distributed ledger technology (DLT) includes blockchain technologies and smart contracts.

While distributed ledgers existed prior to Bitcoin, the Bitcoin blockchain marks the convergence of a host of technologies, including timestamping of transactions, Peer-to-Peer (P2P) networks, cryptography, and shared computational power, along with a new consensus algorithm.

আমাদের কাছে নির্দিষ্ট স্থানে দুটি পাঠ্য ফাইল রয়েছে এবং প্রোগ্রামটি এই দুটি ফাইলের বিষয়বস্তু পড়ে এবং কনসোলে পাঠ্য প্রদর্শন করে৷


  1. কিভাবে একটি Tkinter টেক্সট উইজেট থেকে সবকিছু মুছে ফেলা যায়?

  2. পাইথনে tkinter-এর লেবেলে পাঠ্যকে কীভাবে ন্যায়সঙ্গত করা যায় tkinter-এ ন্যায্যতা প্রয়োজন?

  3. কিভাবে একটি উইন্ডো থেকে Tkinter উইজেট মুছে ফেলা যায়?

  4. পাইথনের একটি লেবেল থেকে পাঠ্য কীভাবে সরানো যায়?