Skip to content

Instantly share code, notes, and snippets.

@wichopy
Created February 10, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wichopy/afbf7b4e09d85dc65f390ad7365a91c4 to your computer and use it in GitHub Desktop.
Save wichopy/afbf7b4e09d85dc65f390ad7365a91c4 to your computer and use it in GitHub Desktop.
scan network folders and populate a csv file with file names, date modified and folder locations.
import os
import csv
import datetime
import timeit
import pandas as pd
import pickle
flcount = 0
addcount = 0
lastadded = 0
datenow = str(datetime.datetime.now().strftime('%Y%m%d'))
folders = pd.DataFrame(columns=('current_dir','fname','fsize'))
filesdir = 'C:\\Users\\chouw\\Documents\\Python Scripts\\TestPicturesScript'
timestart = timeit.default_timer()
print "Initiating File Count Script.."
csvfile = open(datenow+'file_extraction.csv', 'wb') #Open CSV file.
writer = csv.writer(csvfile)
for dirName, subdirlist, fileList in os.walk(filesdir):
current_dir = dirName.replace(filesdir,"") #shorten folder path for smaller csv file.
for fname in fileList:
fsize = os.path.getsize(os.path.join(dirName,fname))
folders.loc[flcount] = [current_dir,fname,current_dir[0:7]]
endtime = timeit.default_timer()
writer.writerow([current_dir,fname.encode('utf-8'),fsize,str(endtime-timestart)]) #write each file to CSV file
flcount +=1
addcount +=1
print (current_dir+" Complete, added: "+str(addcount-lastadded)+" files")
lastadded = addcount
csvfile.close() #Close CSV file after for loop finishes.
endtime = timeit.default_timer()
#show run time and total file count.
print folders['current_dir'].shape
print "added files: "+str(addcount)
#Create pickle incase I want to play with the data in a dataframe.
try:
with open('pic_pickle.pkl','wb') as f:
pickle.dump(folders,f)
f.close()
print "created pickle"
except:
print "dumping to pickle didnt work"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment