I have a script that looks for specific images in a network drive, copies them, and zips them into a separate folder (assuming everything is where it needs to be). This worked great when I was testing this on the same set of images locally on my machine; it zips through in less than a second. But then I connected it to the network drive--same exact set of images--and now it inexplicably takes several minutes.
I'm inclined to assume it's the network drive and not my script... but just to be sure, is there anything I'm doing that might potentially be causing a problem? e.g., I'm fairly new to os.walk; is it possible I'm looking in irrelevant folders that's wasting time? (I don't think I am, but)
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