开发者

Find and copy files to existing Folders with corresponding names in python

开发者 https://www.devze.com 2023-02-28 06:40 出处:网络
i have a large number of files that i need to move to a specific existing folder. the problem is that the number code that relates the file and the folder has text on either side of it as shown below

i have a large number of files that i need to move to a specific existing folder. the problem is that the number code that relates the file and the folder has text on either side of it as shown below the number code is a071137 in the file name and A071137 in the folder name.

for example, i need to move file: "a071137_gindalbie.pdf" to folder "A071137 2006 Te开发者_StackOverflow中文版ck Commginco Pty Ltd" .

I would like to do this using python

I guess I first find the corresponding file using glob? Then to copy it there ?? not sure??


Your question is rather vague, but hopefully I can at least point you in the right direction here. Sounds like you will want to be able to list all of the files in a directory, which can be done with os.listdir(directory) as described in the api. You will likely then iterate through these files, and extract the relevant information you will need using a regular expression. You'll need to reference the api on that too. For example, you can extract a number using something like:

>>> import re
>>> r = re.compile('^a(?P<id>[0-9]+)')
>>> m = r.match('a123')
>>> m.group('id')
'123'

Next you can move files using things in the shutil module. You should be able to use shutil.move() for your purposes. For more information, again, checkout the api.

0

精彩评论

暂无评论...
验证码 换一张
取 消