python


Python - Copying Most Recent File From Folder with SubDirectories


I'm trying to copy the most recent file from a series of folders. Here's the structure:
\\host\data\folder1\*.bk
\\host\data\folder2\*.bk
\\host\data\folder3\*.bk
\\host\data\folder4\*.bk
There are about 600 of these folders. I want to copy the most recent file from each folder to a single folder. Some folders could be empty as well.
I'm totally lost here and tried lots of things with no luck. This should be easy and I'm not sure why I'm having such a big issue.
Basic code,
import os, shutil, sys
source = r"\\server\data"
dest = r"e:\dest"
for pth in os.listdir(source):
if "." not in pth:
newsource = source + "\\" + pth + "\\"

I wrote the below in a text editor, so I wasn't able to fully test it; but this should get you most of the way there.
import os
import operator
source = r"\\server\data"
destination = r"e:\dest"
time_dict = {}
#Walk all of the sub directories of 'data'
for subdir, dirs, files in os.walk(source):
#put each file into a dictionary with thier creation time
for file in os.listdir(dir):
time = os.path.getctime(os.path.join(subdir,file))
time_dict.update({time,file})
#sort the dict by time
sorted_dict = sorted(time_dict.items(), key=operator.itemgetter(0))
#find the most recent
most_recent_file = next(iter(sorted_dict))
#move the most recent file to the destination directory following the source folder structure
os.rename(source + '\\' + dir + '\\' + most_recent_file,str(destination) + '\\' + dir + '\\' + most_recent_file)


Related Links

Why is get_group so slow in pandas?
python gnuplot read csv file to plot time in x-axis in the read order or row order
Error in the tamplate with Django : can I make arithmetic in if statement
Twisted throws “Can only pass-through bytes on Python 2” for no reason
django - urlshortener app
django adding part of url to static css path
Write Raw Numbers to Disk
Parsing fixed width files with special characters inside the file?
Python dateutil rrule error
Replace WhiteSpace with a 0 in Pandas (Python 3)
Just like the BeautifulSoup example…what's wrong?
What is the difference print a object directly and print a object after call str function in python
Debug python that wont respect a catch statement
xlsxwriter & xlrd issue - xlsx file saved as ZIP on one machine, but with other first 4 bytes on another
How do I access values from python social auth used in template
Understanding len function with iterators

Categories

HOME
sharepoint
elixir
npm
oauth
amazon-swf
warnings
bower
otrs
deep-linking
rocketmq
propertygrid
quill
ng-admin
aruco
jboss-eap-7
ssr
camera-calibration
squarespace
equalizer
scaling
bootstrap-popover
cairo
atlassian-plugin-sdk
zoho
multiple-columns
epicor
countif
restfb
compare-and-swap
visual-c++-2017
lync-2013
cep
mef2
data-conversion
aurelia-binding
m2e
rhomobile
reactivemongo
honeysql
openshift-enterprise
lego
boilerplate
forecasting
strstr
active-model-serializers
visualstudio.testtools
infusionsoft
twiki
android-preferences
sendgrid-templates
xbim
android-maps-v2
spring-bean
3scale
uiviewpropertyanimator
spell-checking
bnd
rpostgresql
bettercms
emgu
payu
sql-import-wizard
system.web.ui.webcontrols
pdflib
windows-azure-pack
json-schema-validator
zurb-foundation-apps
nofollow
unsatisfiedlinkerror
gradle-eclipse
clob
spring-repositories
string-parsing
except
ninject.web.mvc
lmfit
ffserver
digits
rspec3
shift-jis
google-plus-one
es2015
uptodate
musl
start-job
utf-32
fragment-tab-host
iostream
log4cplus
code-testing
valence
config-files
json-patch
android-json-rpc
operations
pyjade
real-time-updates
osx-snow-leopard
dig
dwolla
fortran77
android-contextmenu
attachevent
qsqltablemodel
feof
datarepeater
tfs-power-tools
pstree
file-exists
nintendo
selectonemenu
throttling
galaxy-tab
adsl
pureftpd
out-of-browser
front-controller
multibyte-functions
filetime
outlook-form





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm