البرمجة

كشف مواقع فولدرات Excel في Python

To achieve this in Python, you can use the os module to traverse through all the subdirectories of your root folder, check if they contain an Excel file, and then export the location of those folders to a separate text file. Here’s an example of how you can do this:

python
import os root_folder = 'D:/Project' excel_files = [] # Traverse through all subdirectories for foldername, subfolders, filenames in os.walk(root_folder): # Check if any Excel file exists in the current subdirectory if any(filename.lower().endswith('.xlsx') or filename.lower().endswith('.xls') for filename in filenames): excel_files.append(foldername) # Export the locations of folders with Excel files to a text file with open('folders_with_excel_files.txt', 'w') as file: for folder in excel_files: file.write(folder + '\n') print("Locations of folders with Excel files have been exported to 'folders_with_excel_files.txt'")

In this example, replace 'D:/Project' with the path to your root folder. The script will then search for Excel files in all subdirectories of the root folder and export the locations of folders containing Excel files to a text file called 'folders_with_excel_files.txt'.

المزيد من المعلومات

لنستكمل الشرح، في هذا السكربت نستخدم وحدة os للتنقل في جميع الفولدرات الفرعية للمجلد الرئيسي، ونتحقق من وجود ملف Excel في كل فولدر فرعي. إذا وجد الملف، سيتم إضافة مسار الفولدر إلى قائمة المسارات excel_files. بعد ذلك، يتم كتابة مسارات الفولدرات التي تحتوي على ملفات Excel إلى ملف نصي بإسم 'folders_with_excel_files.txt'.

يمكنك تغيير اسم الملف الرئيسي واستبدال 'D:/Project' بالمسار الخاص بالمجلد الرئيسي لديك، ويجب أن تكون الإمتدادات المدعومة لملفات Excel موجودة في القائمة التي تلي الجملة any() في الكود.

هذا السكربت سيكون مفيداً لتحليل هيكلية المجلدات والملفات في مشروعك ومعرفة الفولدرات التي تحتوي على ملفات Excel بشكل سريع وفعال.

زر الذهاب إلى الأعلى