通过Python连接到网络驱动器需要很长时间

2024-09-30 16:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,可以在网络驱动器中查找特定的图像,复制它们,然后将它们压缩到一个单独的文件夹中(假设所有内容都在需要的地方)。当我在我的机器上对同一组图像进行本地测试时,这个方法非常有效;它在不到一秒钟的时间内就完成了。但后来我把它连接到网络驱动器上--同样的一组图像--现在它莫名其妙地需要几分钟。你知道吗

我倾向于假设它是网络驱动器而不是我的脚本。。。但我要确定的是,我所做的任何事情都有可能引起问题吗?e、 g.,我对这方面还比较陌生os.步行;是否有可能我在不相关的文件夹中查找,这是在浪费时间?(我想我不是,但是)

    import pyodbc, shutil, zipfile, os, pandas as pd
    lanid = input('Please enter your lanid.')
    src = blah\\blah\\blah\\photos
    dst = "C:\\Users\\"+lanid+"\\Documents\\survey"
    os.chdir(src)


    if not os.path.exists(dst):
        os.makedirs(dst)

    [...some stuff about connecting to a sql database here...]

#looks for images in the network drive based off a table taken from the sql database.  if the images exist, they're copied into another folder; if they're not, it adds the missing images to a dictionary, to be reported out on later
    missingimages = {}
    def copyimages():
        for index, row in df.iterrows():
            personalityID = row['personalityID']
            personalityID = str(personalityID)
            name = row['name']
            try:
                shutil.copy(str(personalityID)+'.jpg', dst)
            except:
                try:
                    shutil.copy(str(personalityID)+'.png', dst)
                except:
                    try:
                        shutil.copy(str(personalityID)+'.jpeg', dst)
                    except:
                        missingimages[personalityID] = name
                        continue
        return missingimages

#if the missingimages dictionary is empty, the copied images are zipped.                       
    def zipimages():        
        if not bool(missingimages):
            zipzip = zipfile.ZipFile("C:\\Users\\"+lanid+"\\Documents\\surveyID_"+surveyID+"_"+date+".zip", mode='w')
            for foldername, subfolders, filenames in os.walk(dst):
                for filename in filenames:
                    zipzip.write(filename)
            zipzip.close()
            print("The file is zipped and ready to go!")
            status = 'completed'

#if the dictionary indicates some images are missing, then it says so
        else:
            print("There are no images for the following personalities.  Please save images of them in the Photos folder. \n")
            missingnames = pd.DataFrame.from_dict(missingimages,orient='index')
            missingnames.reset_index(inplace=True)
            missingnames.columns = ['personalityID','name']
            print(missingnames)
            status = 'not completed'
        return status

#run the above functions   
    copyimages()
    status = zipimages()

#prompts the user to restart the script if it didn't complete successfully
    while status == 'not completed':
        cont = str(input('\n Some images are missing.  Once they have been placed in the Photos folder, please enter Y to restart the script.'))
        if cont == 'Y':
            copyimages()
            status = zipimages()
        elif cont != 'Y':
            break

Tags: thetoinforifosstatusnot