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

时间:2017-07-18 03:06:36

标签: python shutil network-drive

我有一个脚本,在网络驱动器中查找特定图像,复制它们,并将它们压缩到一个单独的文件夹中(假设一切都在它需要的位置)。当我在我的机器上本地测试同一组图像时,这非常有用;它会在不到一秒的时间内完成。但后来我将它连接到网络驱动器 - 同样精确的图像集 - 现在它莫名其妙地需要几分钟。

我倾向于认为它是网络驱动器,而不是我的脚本...但只是为了确定,有什么我正在做的可能会导致问题吗?例如,我对os.walk相当新鲜;我是否有可能在寻找浪费时间的无关文件夹? (我不认为我,但是)

    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

0 个答案:

没有答案