I’m using a Python script in EventGhost to match certain file types in a directory and move them to certain places for other programs to perform actions on them. Here’s the entire script:
import shutil
import os
SubFileTypes = ('sub','srt','txt')
ZipFileTypes = ('rar','zip','7z','r0')
MediaFileTypes = ('mkv','avi','mp4','wmv')
DownloadName = ''.join(eg.event.payload)
FileName = os.path.basename(DownloadName)
isFolder = os.path.isdir(DownloadName)
eg.globals.tvzip = 'J:\\DL\\TVzip\\'
eg.globals.tvzipdir = eg.globals.tvzip+FileName+'\\'
eg.globals.tvproc = 'J:\\DL\\TVProc\\'
if isFolder == True:
os.mkdir(eg.globals.tvzipdir)
# print 'I\'m a folder!'
for root, dirs, files in os.walk(DownloadName):
for f in files:
if f.endswith(ZipFileTypes):
#print 'I\'m a zip file!'
shutil.copy(os.path.join(root,f),eg.globals.tvzipdir)
if f.endswith(SubFileTypes) or f.endswith(MediaFileTypes):
#print 'I\'m a subtitle or media file!'
shutil.copy(os.path.join(root,f),eg.globals.tvproc)
elif isFolder == False:
shutil.copy(DownloadName,eg.globals.tvproc)
eg.plugins.EventGhost.DisableItem(XmlIdLink(23))
# print 'I\'m NOT a folder!'
else:
print 'I dont know what I am!'
The specific problem I’m having is that I need the ability to match each .rX extension that comes from a split-rar format. These extensions start at r0 and can end at an unlimited number. They are at minimum “r+two digits” (r00,r01,r02, etc) but I think they can get above two digits, though I’m not positive.
Is there some way I can alter my ZipFileTypes list to include these split-rar extensions? Or is there another way?
You can use a regex to match filenames ending in
.rfollowed by any number of digits:re.search()will look for a match anywhere in the string, whilere.match()will look for a full string match. For this case, because we only care about the file extension, we’re going to usere.search().The regular expression is structured as follows:
\.r– matches a single period, followed by anr. The\to escape is necessary because.means wildcard otherwise.\d+– matches any number of digits.\drepresents a digit,+represents “1+ of the previous”$– matches the end of a string.Put them all together into
\.r\d+$and you match a split rar extension.