为了账号安全,请及时绑定邮箱和手机立即绑定

对于其他列表和文件夹传输

对于其他列表和文件夹传输

潇潇雨雨 2023-10-06 18:33:56
我的子文件夹中有多个文件需要复制到另一个具有指定子文件夹名称和日期时间的主文件夹,意味着A1/B/C/D/D.txt复制到A2/B/C/D/datetime_username/D.txt,目录 B、C、D 中有多个文件夹,也许是 B1、B2...用户决定要传输哪个子文件夹现在我正在尝试打印 D 文件夹的路径。我尝试的文件夹位于我的桌面上。    import os, shutil        os.chdir('C:\\path')        def main():        lvl1 = ['Typ','GFP2']        lvl2 = ['A0','A1']        lvl3 = ['SCAN','MBIST']            project = input("Enter project: ")                #Validation        for s in lvl1:                if s == project:                src1 = 'IDC\\{}'.format(s)                                stepping = input("Enter Stepping: ")                                for l2 in lvl2:                    if l2 == stepping:                        #shutil.copytree()                        src2 = src1+'\\{}'.format(l2)                            thirdlvl = input("Enter SCAN/MBIST: ")                            for l3 in lvl3:                            if l3 == thirdlvl:                                src3 = src2+'\\{}'.format(l3)                                print(src3)                                                else:                    print("Stepping not found")                    break            else:            print("Project name not found")                    main()我会得到'''Enter project: TypEnter Stepping: A0Enter SCAN/MBIST: SCANIDC\Typ\A0\SCANStepping not found'''如果我想仅在未找到步进时打印“未找到步进”,现在即使存在此文件夹也会打印它。谢谢
查看完整描述

1 回答

?
慕虎7371278

TA贡献1802条经验 获得超4个赞

您需要break在您的for-else. 最小修改是:


def main():

    lvl1 = ['Typ','GFP2']

    lvl2 = ['A0','A1']

    lvl3 = ['SCAN','MBIST']


    project = input("Enter project: ")

    

    #Validation

    for s in lvl1:


        if s == project:

            src1 = 'IDC\\{}'.format(s)

            

            stepping = input("Enter Stepping: ")

            

            for l2 in lvl2:

                if l2 == stepping:

                    #shutil.copytree()

                    src2 = src1+'\\{}'.format(l2)


                    thirdlvl = input("Enter SCAN/MBIST: ")


                    for l3 in lvl3:

                        if l3 == thirdlvl:

                            src3 = src2+'\\{}'.format(l3)

                            print(src3)

                    break

            else:

                print("Stepping not found")

                

            break

    else:

        print("Project name not found")

for-loop您还可以通过以下方式摆脱


def main():

    lvl1 = ['Typ','GFP2']

    lvl2 = ['A0','A1']

    lvl3 = ['SCAN','MBIST']


    project = input("Enter project: ")


    #Validation

    if project in lvl1:

        src1 = 'IDC\\{}'.format(project)

        

        stepping = input("Enter Stepping: ")

        

        if stepping in lvl2:

            #shutil.copytree()

            src2 = src1+'\\{}'.format(stepping)


            thirdlvl = input("Enter SCAN/MBIST: ")


            if thirdlvl in lvl3:

                src3 = src2+'\\{}'.format(thirdlvl)

                print(src3)

        else:

            print("Stepping not found")

    else:

        print("Project name not found")

if-else不需要多层结构。这是一个修改。我还使用os.path.join()比字符串操作更适合处理路径:


import os, shutil


working_dir = os.path.join("C:/", "path")

os.chdir(working_dir)


def main():

    lvl1 = ['Typ','GFP2']

    lvl2 = ['A0','A1']

    # lvl3 = ['SCAN','MBIST']

    

    project = input("Enter project: ")

    if project not in lvl1:

        print("Project name not found")

        return

    

    stepping = input("Enter Stepping: ")

    if stepping not in lvl2:

        print("Stepping not found")

        return


    thirdlvl = input("Enter SCAN/MBIST: ")

    

    src = os.path.join(project, stepping, thirdlvl)

    print("src", src)

    

    if not os.path.exists(src):

        print("Src not exists")


main()

os.listdir()您甚至可以通过为您列出所有子文件夹来进一步自动化该过程。


import os, shutil


working_dir = os.path.join("C:/", "path")

os.chdir(working_dir)


def main():    

    project = input("Enter project: ")

    lvl1 = os.listdir(working_dir) # list all the directories under the working directory.

    if project not in lvl1:

        print("Project name not found")

        return # if you want to exit when not finding the project folder.

    

    stepping = input("Enter Stepping: ") # list all the directories under the project directory.

    lvl2 = os.listdir(os.path.join(working_dir, project))

    if stepping not in lvl2:

        print("Stepping not found")

        return # if you want to exit when not finding the stepping folder.

        

    thirdlvl = input("Enter SCAN/MBIST: ")

    

    src = os.path.join(project, stepping, thirdlvl)

    print("src", src)

    

    if not os.path.exists(src):

        print("Src not exists")


main()


查看完整回答
反对 回复 2023-10-06
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信