英文:
Copying specific files based on multiple strings with file name in Python
问题
I want to copy specific files based on string what I assign to another directory.
我的目标是根据我分配的字符串复制特定文件到另一个目录。
My question similar to text
我的问题类似于text
But in my loc folder have many file like:
但是在我的loc文件夹中有很多文件,例如:
File name is different.
文件名是不同的。
So far I'm not able to copy files successfully with the below code also:
到目前为止,我也无法成功复制文件,使用下面的代码:
I want get like below in my des folder when I assign pattern is "100000" and "500000".
当我分配模式为"100000"和"500000"时,我希望在我的des文件夹中获得如下文件:
Can anyone help? Thanks in advance!
有人可以帮助吗?提前感谢!
AttributeError Traceback (most recent call last)
AttributeError跟踪(最近的调用)
... (the rest of the traceback)
...(剩余的跟踪信息)
英文:
I want to copy specific files based on string what I assign to another directory.
My question similar to text
But in my loc folder have many file like:
File name is different.
20220101(100000-19999).xlsx
20220101(200000-29999).xlsx
20220101(300000-39999).xlsx
20220101(400000-49999).xlsx
20220101(500000-69999).xlsx
20220101(600000-69999).xlsx
20220101(700000-79999).xlsx
20220101(800000-89999).xlsx
20220101(900000-99999).xlsx
20220101(1000000-109999).xlsx
20220101(1100000-119999).xlsx
So far I'm not able to copy files successfully with the below code also:
import shutil
import os
import glob
loc= 'C:/path/to/files/folder'
des = 'G:/path/to/files/new_folder'
pattern= ["100000", "500000"]
patterns= [f"20220101({i}*" for i in pattern]
files = Path(loc).rglob(patterns)
for file in files:
print(file)
shutil.copy(file, des )
I want get like below in my des folder when I assign pattern is "100000" and "500000".
20220101(100000-19999).xlsx
20220101(500000-69999).xlsx
Can anyone help? Thanks in advance!
AttributeError Traceback (most recent call last)
Cell In[16], line 10
5 patterns= [f"20220101({i}*" for i in pattern]
8 files = Path(loc).rglob(patterns)
---> 10 for file in files:
11 print(file)
12 shutil.copy(file, des)
File ~\anaconda3\lib\pathlib.py:1043, in Path.rglob(self, pattern)
1038 """Recursively yield all existing files (of any kind, including
1039 directories) matching the given relative pattern, anywhere in
1040 this subtree.
1041 """
1042 sys.audit("pathlib.Path.rglob", self, pattern)
-> 1043 drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
1044 if drv or root:
1045 raise NotImplementedError("Non-relative patterns are unsupported")
File ~\anaconda3\lib\pathlib.py:66, in _Flavour.parse_parts(self, parts)
64 continue
65 if altsep:
---> 66 part = part.replace(altsep, sep)
67 drv, root, rel = self.splitroot(part)
68 if sep in rel:
AttributeError: 'list' object has no attribute 'replace'
答案1
得分: 1
import shutil
from pathlib import Path
loc = 'C:/path/to/files/folder'
des = 'G:/path/to/files/new_folder'
pattern = ["100000", "500000"]
patterns = [f"20220101({i}*" for i in pattern]
files = []
for pat in patterns:
files = files + sorted(Path(loc).rglob(pat))
for file in files:
print(file)
shutil.copy(file, des)
Cause rglob takes str as arg, but which you give was a list.
英文:
I suppose it can work in this way.
import shutil
from pathlib import Path
loc = 'C:/path/to/files/folder'
des = 'G:/path/to/files/new_folder'
pattern = ["100000", "500000"]
patterns = [f"20220101({i}*" for i in pattern]
files = []
for pat in patterns:
files = files + sorted(Path(loc).rglob(pat))
for file in files:
print(file)
shutil.copy(file, des)
Cause rglob takes str as arg, but which you give was a list.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论