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

Assign and compare in python
List comprehension won't return expected output
Accessing django oscar product attributes
Incorrectly Replacing Content
Python Max Recursion Depth
Error loading IPython notebook
Accessing Python webserver remotely on Amazon EC2
Efficient combined in-place adding/removing of rows of a huge 2D numpy array
PANDAS: fast check whether integers fall into a set of intervals
python hex-editor binary change
Out of Memory with MultiPart Post, Python 2.7
How to ignore objects that don't satisfy the “class” conditions?
SaltStack client.cmd outputter
Confused about try/except with custom Exception
Url fetches in google appengine live
django hyperlink in custoom middleware not workiing

Categories

HOME
jdbc
erlang
wxwidgets
tomcat7
jscript
apple-numbers
jpanel
facebook-javascript-sdk
pjsip
nixos
google-shopping
intellij-plugin
informatica-powercenter
squarespace
cratedb
netezza
saiku
aws-cognito
ghost-inspector
tcpclient
azure-servicebus-queues
data-cleansing
spring-data-neo4j
bitcoin-testnet
jackson-dataformat-csv
pyscripter
qtablewidget
recurrence-relation
reactivemongo
gettext
hashcat
pmwiki
poco-libraries
initializer
code-behind
rule
ddms
nashorn
spring-bean
resuming-training
mod-fcgid
z3py
ajp
chown
uiviewpropertyanimator
heroku-postgres
autoresize
restivejs
barcode-printing
cfeclipse
mongoose-populate
gameanalytics
contour
supportmapfragment
mfc-feature-pack
web-essentials
except
ninject.web.mvc
application-loader
urn
prezto
coypu
ibm-data-studio
xaml-designer
subversion-edge
searchkick
magento-1.12
pl-i
orientation-changes
symfony-2.6
docopt
power-law
indexing-service
joox
delphi-xe3
visualstatemanager
help-viewer
zipalign
bounds
flash-cc
cling
logentries
convex-polygon
cdata
trimming
stagefright
xslkey
appfog
attachevent
blockingqueue
fotoware
cosm
emacs23
fileutils
associative
hadoop-plugins
scala-2.8
evb

Resources

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
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App