在角色和符号之间获取字符串,而不必关心以下符号的内容
为了将某些文件订购到文件夹中,我必须获取两个文件夹的数字(好像是某种ID)(称为px,p。Ixeced,为x一个数字,x一个数字,范围为1至200150)和文件(为px_n.gmspr,p是固定的,x是文件夹的ID号,n是文件的标识符,可以为2,3,6,8,9,a和h)。
一个示例是p.24和p24_2.gmspr,p24_3.gmspr,p24_6.gmspr,p24_8.gmspr,p24_9.gmspr,p24_a.gmspr和
p24_h.gmspr .GMSPR文件与目标文件夹PX不同。一点点OS.CHDIR和OS.RENAME和文件可以轻松移动,因此我认为这不是问题。
我想要的是获取文件名的X号码以与文件夹号进行比较,而忘记了p和_n.gmspr字符串。 而我可以通过 foldername.split(“。”,1)[1]
我真的不知道该如何为文件编号做。
总而言之,我想将一些称为px_n.gmspr的文件移动到另一个文件夹几乎确定了同一PX
?谢谢你!!!
编辑: 关于给出的答案,我必须澄清自己要做的事情,特别是使用文件和文件夹格式:
Mother folder
├── Unclassified
│ └── All PX_N.gmspr being PX certain files that gotta be moved to another folders, X a number that ranges from 1 to 200150 (but not exactly 200150, is just a number ID) and N can be only 2, 3, 6, 9, A or H, nothing more. In total 15435 elements with each of the X having one of the 6 possibles N gmspr.
├──First Folder
│ └── p.X folders (X from 1 to 151), the aim is to select all the PX_N.gmspr files that agree with the X number that matches the p.X of the folder and move it to each folder.
├──Second Folder
│ └── p.X folders (X from 152 to 251, plus p.602 to p.628, p.823, p.824,
│ p.825, p.881 and p.882)
└──Third Folder
└── p.X folders (X from 252 to 386, plus p.585, p.586 and p. 587)
还有其他一些文件夹来订购更多15435个文件中的一些文件夹。 我目前正在搜索Regex;对我来说不幸的是,这是我第一次真正必须使用它们。
编辑原因解决了:关键是要使用正则高音并仅获取数字,但是随着嵌套列表的出现,只有第一个数字很有用
In order to order some files into folders, I have to get the number (as if it was some sort of ID) of both folders (named as p.X, p. fixed, being X a number that can range from 1 to 200150) and files (being PX_N.gmspr, where P is fixed, X is the ID number of the folder and N an identifier of the file, which can be 2,3,6,8,9,A and H).
An example would be p.24 and P24_2.gmspr, P24_3.gmspr, P24_6.gmspr, P24_8.gmspr, P24_9.gmspr, P24_A.gmspr and P24_H.gmspr, in order to move all P24_N.gmspr to p.24
The PX_N.gmspr files are in a different folder than the target folders p.X . A little of os.chdir and os.rename and the files can be moved easily so I believe that is not a problem.
What I want is to obtain the X number of the filename to compare with the folder number, forgetting about both the P and the _N.gmspr string.
Whereas I can obtain the folder number viafoldername.split(".",1)[1]
I don't really know how to do it for the file number.
To sum up, I want to move some files called PX_N.gmspr to another folder identified almost the same p.X
Any idea? Thank you!!!
EDIT:
Regarding the answer given, I have to clarify myself about what I am trying to do, specially with the file and folder format:
Mother folder
├── Unclassified
│ └── All PX_N.gmspr being PX certain files that gotta be moved to another folders, X a number that ranges from 1 to 200150 (but not exactly 200150, is just a number ID) and N can be only 2, 3, 6, 9, A or H, nothing more. In total 15435 elements with each of the X having one of the 6 possibles N gmspr.
├──First Folder
│ └── p.X folders (X from 1 to 151), the aim is to select all the PX_N.gmspr files that agree with the X number that matches the p.X of the folder and move it to each folder.
├──Second Folder
│ └── p.X folders (X from 152 to 251, plus p.602 to p.628, p.823, p.824,
│ p.825, p.881 and p.882)
└──Third Folder
└── p.X folders (X from 252 to 386, plus p.585, p.586 and p. 587)
There are some other folders in order to order some more of the 15435 files.
I am currently searching about regex; unluckily for me, it is the first time I actually have to use them.
EDIT CAUSE SOLVED: SO THE POINT WAS TO PLAY WITH REGEX AND GETTING ONLY THE NUMBERS, BUT THEN AS NESTED LISTS APPEARED, ONLY THE FIRST NUMBER WAS USEFUL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是Regexes的完美工作。
首先,让我们创建一个临时的dir,并用一些文件填充以演示。
现在,我们有4种类型的文件(PN。),此类文件在1到5个文件之间。
然后,我们只需要遍历这些文件,用the Regex
>
) p(\ d+)\ ..+
,最后创建目标dir并移动文件。
例如,从平坦的临时目录中,我们现在进行了以下排序。
最后,清理临时目录。
This is the perfect job for regexes.
First, let's create a temporary dir and fill it with some files to demonstrate.
Now we have 4 types of file (PN.), with between 1 and 5 files in this type.
Then, we just need to iterate through those file, extract the N from the file name with the regex
P(\d+)\..+
, and finally create destination dir and move the file.For instance, from a flat temp directory, we have now the following sorted.
And finally, cleanup the temporary directory.