我正在尝试在 Python 中创建一个脚本来组织我的 torrent 下载文件夹。我的文件夹结构包含带有文件的文件和文件夹(多个 lvls)。这些文件具有不同的文件格式,但如果文件夹包含任何 MP4 类型的文件,则应将其移至视频文件夹。如果文件夹包含 mp3/audioformat 到 Audiofolder。我设法为第一级目录制作了脚本。但是很难递归地做到这一点。尝试过 glob,os.walk。glob 由于 fildir 名称而给出错误。在 os.walk 中,如果子目录包含特定的文件类型,我无法找到移动顶层目录的方法。
import os
import shutil
import glob
numberFiles = 0
totalObjects = 0
numberDir= 0
pathToDir = '/mybookone/seedbox1/files/' # this is the dir containing top lvl files and folders
isEbook = 0
isAudio = 0
isAudioDir = 0
bookExtensions = ('.epub', '.mobi','.pdf','.azw3','.djvu')
audioExtensions = ('.m4b', '.mp3')
ebookEndPath = '/mybookone/seedbox1/fileorgx/ebooks'
audiobookEndPath = '/mybookone/seedbox1/fileorgx/audiobooks'
for f in os.listdir(pathToDir):
if (f == 'fileorgx'):
continue
totalObjects = totalObjects + 1
if os.path.isfile(pathToDir + f):
numberFiles = numberFiles + 1
if(f.endswith(bookExtensions)):
isEbook += 1
shutil.move(pathToDir + f, ebookEndPath)
if(f.endswith(audioExtensions)):
isAudio += 1
shutil.move(pathToDir + f, audiobookEndPath)
if os.path.isdir(pathToDir + f):
numberDir = numberDir + 1
isAudioDir = 1
for ext in audioExtensions:
pathFolderEsc = pathToDir
for r,d,f in os.walk(pathToDir + f):
for i in f:
print(os.path.join(r,i))
#shutil.move(pathToDir + f, audiobookEndPath)
# should be able to move top lvl folder here. But can only manage to get the folder containing the file. And also maybe stop the loop for the current top lvl dir. So I only ever want to move the first level folder (and its content) in the download directory.
print('files:',numberFiles , 'dirs:' , numberDir , 'total:' , totalObjects, 'epub:', isEbook, 'audiobooks', isAudio, 'audioDir:', isAudioDir)
非常感谢您的帮助。