使用 Python 匹配 split-RAR 扩展

发布于 2024-12-29 09:22:54 字数 1397 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身软味 2025-01-05 09:22:54

您可以使用正则表达式来匹配以 .r 结尾的文件名,后跟任意数量的数字:

import re

# -snip-

    for f in files:
        if f.endswith(ZipFileTypes) or re.search(r'\.r\d+

re.search() 将在字符串中的任何位置查找匹配项,而 < code>re.match() 将查找完整的字符串匹配。对于本例,因为我们只关心文件扩展名,所以我们将使用 re.search()

正则表达式的结构如下:

  • \.r - 匹配单个句点,后跟 r。转义的 \ 是必需的,因为 . 表示通配符。
  • \d+ - 匹配任意数量的数字。 \d 表示数字,+ 表示“前一个的 1+”
  • $ - 匹配字符串的结尾。

将它们全部放入 \.r\d+$ 中,您就可以匹配拆分的 rar 扩展名。

, f): # do stuff

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:

import re

# -snip-

    for f in files:
        if f.endswith(ZipFileTypes) or re.search(r'\.r\d+

re.search() will look for a match anywhere in the string, while re.match() will look for a full string match. For this case, because we only care about the file extension, we're going to use re.search().

The regular expression is structured as follows:

  • \.r - matches a single period, followed by an r. 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.

, f): # do stuff

re.search() will look for a match anywhere in the string, while re.match() will look for a full string match. For this case, because we only care about the file extension, we're going to use re.search().

The regular expression is structured as follows:

  • \.r - matches a single period, followed by an r. 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.

赴月观长安 2025-01-05 09:22:54

使用正则表达式,我不知道如何在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.

请爱~陌生人 2025-01-05 09:22:54

使用旧的命名模式,分割档案遵循以下语法:

.rar、.r00 - .r99、.s00 - .s99、.t00 - .z99、.{00 - .{99、.|00 ...

您可以使用以下命令进行测试:

rar a -v0.1m -vn -m0 test.rar testfile

Windows 在尝试执行以下操作时会提示错误创建存档 .|00。此外,这并不重要,因为从 .rar.z99 有 901 个部分。在正常情况下永远不应该达到这一点。

但我见过用 .s[xx] 分割档案,所以我推荐这个正则表达式:

r'\.[r-z]{1}(([0-9]{2})|ar)

并且为了提高一点速度,可以这样使用它:

import re

#some code

#compile the regex
reg_rar = re.compile(r'\.[r-z]{1}(([0-9]{2})|ar)

并且为了提高一点速度,可以这样使用它:


)
for root, dirs, files in os.walk(DownloadName):
    for f in files:
        if reg_rar.search (f) :
            #do sth

并且为了提高一点速度,可以这样使用它:

With the old naming schema, split archives follow this syntax:

.rar, .r00 - .r99, .s00 - .s99, .t00 - .z99, .{00 - .{99, .|00 ...

You can test it with:

rar a -v0.1m -vn -m0 test.rar testfile

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:

r'\.[r-z]{1}(([0-9]{2})|ar)

And for little speed improvements use it this way:

import re

#some code

#compile the regex
reg_rar = re.compile(r'\.[r-z]{1}(([0-9]{2})|ar)

And for little speed improvements use it this way:


)
for root, dirs, files in os.walk(DownloadName):
    for f in files:
        if reg_rar.search (f) :
            #do sth

And for little speed improvements use it this way:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文