使用 Python 匹配 split-RAR 扩展
我在 EventGhost 中使用 Python 脚本来匹配目录中的某些文件类型,并将它们移动到某些位置,以便其他程序对它们执行操作。这是整个脚本:
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!'
我遇到的具体问题是我需要能够匹配来自 split-rar 格式的每个 .rX 扩展名。这些扩展从 r0 开始,可以无限数量结束。它们至少是“r+两位数”(r00、r01、r02 等),但我认为它们可以超过两位数,尽管我并不肯定。
有什么方法可以更改我的 ZipFileTypes 列表以包含这些 split-rar 扩展名吗?或者还有别的办法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用正则表达式来匹配以
.r
结尾的文件名,后跟任意数量的数字:re.search()
将在字符串中的任何位置查找匹配项,而 < code>re.match() 将查找完整的字符串匹配。对于本例,因为我们只关心文件扩展名,所以我们将使用re.search()
。正则表达式的结构如下:
\.r
- 匹配单个句点,后跟r
。转义的\
是必需的,因为.
表示通配符。\d+
- 匹配任意数量的数字。\d
表示数字,+
表示“前一个的 1+”$
- 匹配字符串的结尾。将它们全部放入
\.r\d+$
中,您就可以匹配拆分的 rar 扩展名。You can use a regex to match filenames ending in
.r
followed 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.\d
represents 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.使用正则表达式,我不知道如何在Python中准确地做到这一点,但你会想要匹配像 /.r[0-9]*/ 这样的东西,只要你匹配文件名就应该这样做。
use a regex, I don't know how to do it exactly in python but you'll want to match on something like /.r[0-9]*/ should do it as long as you're matching file names.
使用旧的命名模式,分割档案遵循以下语法:
您可以使用以下命令进行测试:
Windows 在尝试执行以下操作时会提示错误创建存档
.|00
。此外,这并不重要,因为从.rar
到.z99
有 901 个部分。在正常情况下永远不应该达到这一点。但我见过用
.s[xx]
分割档案,所以我推荐这个正则表达式:并且为了提高一点速度,可以这样使用它:
With the old naming schema, split archives follow this syntax:
You can test it with:
Windows will prompt an error while it tries to create an archive
.|00
. Furthermore, it doesn't really matter, because from.rar
until.z99
there are 901 parts. That should never be reached in normal cases.But I have seen split archives with
.s[xx]
, so I recommend this regular expression:And for little speed improvements use it this way: