根据某些字符串定义多个路径变量

时间:2019-09-24 03:23:09

标签: python

我有一个代码正在处理并将两个文件夹(A和B)中的文件复制到8个不同的文件夹中,其中文件夹A中的文件进入4个文件夹,文件夹B中的文件进入到其他4个文件夹。

A和B的4个文件夹中的每个都有不同的名称,但文件夹名称中都包含A或B。

如何根据包含字符串的文件夹定义dest_path而不是定义实际路径?

此刻,我列出了dest_paths,但是试图通过包含A或B来定义它们;但无法了解如何将dest_path定义为循环,以将文件从原始A和B文件夹复制到其中

ppath = ("D:\\parent\\path\\to\\all\\content")

#want adest to say any folder that contains a and bdest to say any folder that contains b
adest = [(str(ppath) + "\\" + "A - Dest1"), (str(ppath) + "\\" + "A - Dest2"),
         (str(ppath) + "\\" + "A - Dest3"),(str(ppath) + "\\" + "A - Dest4")]
bdest = [(str(ppath) + "\\" + "B - Dest1"), (str(ppath) + "\\" + "B - Dest2"),
         (str(ppath) + "\\" + "B - Dest3"),(str(ppath) + "\\" + "B - Dest4")]

#####Trying to get paths to be defined by containing A or B#####
search_path = os.path.join(ppath + "\\")
astring = "A"
bstring = "B"
for file in os.listdir(search_path)
    if astring in file:
        dest_path = file
    else:
        dest_path = file

#Parent folder the A and B folders (todays date) and parent folders
dfolder = os.path.join(ppath + "\\" + "190924")
apath = os.path.join(dfolder + "\\" + "A")
bpath = os.path.join(dfolder + "\\" + "B")

#CopyBlock
for filename in os.listdir(dfolder):
    if filename == ("A"):
            for file in os.listdir(apath):
                fulla_name = os.path.join(apath, file)
                for path in adest:
                    shutil.copy(fulla_name, path)

    elif filename == ("B"):
        for file in os.listdir(bpath):
            fullb_name = os.path.join(bpath, file)
            for path in bdest:
                shutil.copy(fullb_name, path)

0 个答案:

没有答案