我正在尝试将文件自动 FTP 到服务器(特定文件夹)。该程序根据某些参数对文件类型进行排序并将文件移动到正确的文件夹,然后 Plex 会处理元数据。
我试图让这个脚本在下载新文件时自动排序。问题是歌曲的专辑(文件夹),被转移到评论文件夹,而不是音乐文件夹。
代码:
import os # walks through directory
import shutil # move files
import time # show runtime
music_filetype = ['.mp3', '.flac', '.m4a', '.wav', '.flac', '.wma', '.aac', '.aiff', '.aif', '.aifc']
video_filetype = ['.mkv', '.mp4', '.m4v', '.mov', '.wmv', '.flv', '.avi', '.mpeg', '.mpg', '.mpe']
archive_filetype = ['.zip', '.rar', '.7z', '.deb', '.tar', '.pkg', '.tar.gz', '.gz', '.rpm', '.z']
# ----------------------------- Need to change directory locations ----- --------------------------
FTP_Folder = r'C:\Users\******\Desktop\Automation Files\Unsorted Files' # Set to where ever the download folder is
Movie_Dest = r'C:\Users\******\Desktop\Automation Files\Movies' # set to where ever the filetype destinations are
TV_Dest = r'C:\Users\******\Desktop\Automation Files\TV'
Music_Dest = r'C:\Users\******\Desktop\Automation Files\Music'
Review_Folder = r'C:\Users\******\Desktop\Automation Files\Review' # if algorithm does not meet parameters, move here
# As of 7/3/2021, this auto sorts everything perfectly into the correct folders, no matter if its a loose file, season
# folder of episodes, folder of songs, band containing album with the songs, movie loose or in a folder.
# The only issue with sorting is an album of songs goes into the movies folder. The weird thing is when running this
# script with just that folder, it sorts just fine putting it in the music folder. When the FTP folder has movies and
# other things with it, then it jacks up.
#
# This tells me its a looping problem of the isMovie() method not exiting in time. Carry over if you will.
# Probably just need to tighten the restrictions or widen the file checker paradigm.
def isMusic(file):
for root, dirs, files in os.walk(FTP_Folder): # will iterate through a directory to get to the bottom most file
for file in files:
for i in music_filetype:
if file.lower().endswith(i):
print("It is music")
return True
else:
return False
def isTV(file):
for root, dirs, files in os.walk(FTP_Folder): # will iterate through a directory to get to the bottom most file
for file in files:
for i in video_filetype:
if file.lower().__contains__('.s0' or '.s1') and file.lower().endswith(i): # For more complexity, '.s' + a number, instead of another
# for loop. Runtime will be too long..
print("It is a TV show")
return True
else:
return False
def isMovie(file):
for root, dirs, files in os.walk(FTP_Folder): # will iterate through a directory to get to the bottom most file
for file in files:
for i in video_filetype:
if not file.lower().__contains__('.s0' or '.s1') and file.lower().endswith(i): # change so it loops
# through the music_filetype list above
print("It is a movie")
return True
else:
return False
def isFile(file): # broken, see "file_mover" method below - and (os.path.isfile(file) not )
#print(__file__)
for i in (video_filetype or music_filetype or archive_filetype):
if os.path.isfile(file) or file.__contains__(i): # checks if file
#print(__file__) # interesting program run results
return True
else:
return False
#if os.path.isdir(file): # checks if directory
#print("It is a directory")
#return False
def file_mover(file_, destination):
name, ext = os.path.splitext(file_)
ext = ext[1:] # This is going to store the extension type
if os.path.exists(destination + '/' + file_): # This checks if file already exists.
# If it does, ideally move it to review folder.
print("File already exists")
shutil.move(FTP_Folder + '/' + file_, Review_Folder + '/' + file_)
print("The file has moved to " + destination)
else:
if isFile(file_):
os.makedirs(destination + '/' + file_) # This will create a new directory.
shutil.move(FTP_Folder + '/' + file_, destination + '/' + file_)
else:
shutil.move(FTP_Folder + '/' + file_, destination + '/' + file_)
print(file_ + " has moved to " + destination)
def filetype_checker(): # Set variable of where to store files, pass through depending on movie, show or music
list_ = os.listdir(FTP_Folder) # This will create a properly organized list with all the filename that is there
# in the directory
for file_ in list_: # This will go through each and every file
ext = os.path.splitext(file_) # Stores the extension type
ext = ext[1:]
if ext == '': # This forces the next iteration, if it is the directory
continue
if isMusic(file_) and not isTV(file_) and not isMovie(file_):
file_mover(file_, Music_Dest)
elif isTV(file_) and not isMusic(file_) and not isMovie(file_): # For more complexity, '.s' + int.str instead of another for loop. Runtime will be too long..
file_mover(file_, TV_Dest)
elif isMovie(file_) and not isTV(file_) and not isMusic(file_):
file_mover(file_, Movie_Dest)
else:
file_mover(file_, Review_Folder)
def main():
start_time = time.time()
filetype_checker()
print("--- %s seconds ---" % (time.time() - start_time))
if __name__ == "__main__":
main()