@צדיק-תמים @אף-אחד-3 החידוש האדיר באמת הוא האפשרות לבקש ממנו לשפר את הקוד שלך:
הנה לדוגמא סקריפט פייתון שמיועד לזהות שם אמן של שיר לפני ואחרי:
לפני:
# -*- coding: utf-8 -*-
import os
import sys
# יבוא פונצקיה לקריאת מטאדאטה של קובץ
import music_tag
# יבוא פונקציה לקריאת עץ תיקיות
from os.path import join, getsize
# יבוא פונצקיה עבור תצוגת האותיות העבריות
from bidi.algorithm import get_display
def pro_scanner(my_file, root):
# הפונקציה סורקת את המטאדאטה של השיר ומכניסה אותו למשתנה
try:
my_file = root + "\\" + my_file
artist_file = music_tag.load_file(my_file)
artist = artist_file['artist']
target_dict[my_file] = artist
except:
pass
def main():
global target_dict
target_dict = {}
dir_path = str(sys.argv[1])
if (dir_path != "") and (os.path.exists(dir_path)):
for root, dirs, files in os.walk(dir_path):
#print("{}".format(root))
#continue
for my_file in files:
pro_scanner(my_file, root)
dict_list = target_dict.items()
for item in dict_list:
file_name = str(item[0])
art_name = str(item[1])
if art_name.isdigit() or art_name.isalpha() or "&" in art_name or art_name == "" or "," in art_name or len(art_name.split()) >= 3:
continue
print("move " + file_name + " to " + art_name)
os.system('pause')
if __name__ == '__main__':
main()
אחרי (כולל תיעוד!):
# -*- coding: utf-8 -*-
import os
import sys
# יבוא פונקציה לקריאת מטאדאטה של קובץ
import music_tag
# יבוא פונקציה לקריאת עץ תיקיות
from os.path import join, getsize
# יבוא פונקציה עבור תצוגת האותיות העבריות
from bidi.algorithm import get_display
def pro_scanner(my_file, root):
"""
פונקציית סורקת את המטאדאטה של השיר ומכניסה את האמן למשתנה
תנאים:
my_file (str) - שם הקובץ שנסרק
root (str) - נתיב התיקייה האב
תוצאה:
מכניס את האמן שנמצא במטאדאטה של השיר למשתנה target_dict
"""
try:
# בדיקה האם הקובץ הוא קובץ MP3
if not my_file.endswith(".mp3"):
return
# יצירת נתיב מלא לקובץ
my_file = root + "\\" + my_file
# טעינת מטאדאטה של השיר
artist_file = music_tag.load_file(my_file)
# קבלת אמן מטאדאטה של השיר
artist = artist_file['artist']
# הכנסת נתוני האמן למשתנה הגלובלי
if artist:
target_dict[my_file] = artist
except:
pass
def main():
"""
הפונקציה המרכיבת את הפקודה הראשית של התכנית. היא סורקת את התיקיות והקבצים תחת נתיב שצוין ומכניסה את האמנים שמופיעים במטאדאטה של השירים למשתנה גלובלי
תנאים:
אין
תוצאה:
מכניס את האמנים שמופיעים במטאדאטה של השירים למשתנה גלובלי
"""
# הגדרת משתנה גלובלי למעט את התגיות הראשיות
global target_dict
target_dict = {}
# קבלת נתיב משתנה
dir_path = str(sys.argv[1])
if (dir_path != "") and (os.path.exists(dir_path)):
for root, dirs, files in os.walk(dir_path):
for my_file in files:
pro_scanner(my_file, root)
dict_list = target_dict.items()
for file_name, artist_item in dict_list:
artist = artist_item.value
if artist.isdigit() or artist.isalpha() or "&" in artist or artist == "" or "," in artist or len(artist.split()) >= 3:
continue
print("move " + file_name + " to " + artist)
if __name__ == '__main__':
main()