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

Convert a complex array of array to a list
sorted on basis of two keys, descending order sort for first and ascending for second
Django + redis session sharing accross multiple hosts
how to access a database of one module from another module
Mqtt subscribe message while continuous publishing to topic
flask-restless validation_exceptions not working for few column in flask-sqlalchemy models
I don't understand how cache work's on GAE python
Get first version of a line with duplicate values versus one column
Using set()/setp() to set unknown properties in matplotlib
Is there a built-in Python function which will return the first True-ish value when mapping a function over an iterable?
Check if some text exists on the webpage using getPageSource. I get error Object has no attribute getPageSource
Detect if connected to the internet using a tethered phone in Python
Sorting a list of list of tuples
Efficient concatenation of byte data in python without creating a huge list?
django two digit integer in choice field
add number to all elements of a list within a class?

Categories

HOME
admin-on-rest
voip
webrtc
jbehave
windows-store-apps
travis-ci
java-home
data-science-experience
nodemailer
requirejs
constructor
keystore
x11
android-source
python-unicode
arm-template
pyephem
propel2
tweets
cruisecontrol.net
jquery-scrollify
pipelinedb
ui5
data-conversion
nesc
dhtmlx-scheduler
devforce
fractals
threshold
broadleaf-commerce
forecasting
es-shell
jquery-multidatespicker
restful-url
exiftool
ssh.net
xbim
microdata
median
crop
jags
seamless-immutable
addin-express
spring-ioc
galleriffic
slam-algorithm
tomcat5
zurb-foundation-apps
multifile-uploader
boost-hana
nssplitview
selecteditem
picking
altbeacon
test-class
p-np
programming-paradigms
ipojo
wif
stream-framework
playscape
twitter-rest-api
crash-dumps
criteria-api
guzzle6
custom-url
magento-1.12
jcr-sql2
inputaccessoryview
hidden-field
humanizer
adaptive-compression
code-testing
bullet
bsp
valence
picat
ifft
slick-2.0
flash-cc
cdata
stagefright
justgage
nuspec
cgpath
git-filter-branch
moq-3
unions
viewdidload
hashalgorithm
sendfile
chatroom
drawtext
getresource
msn
camtasia
integer-promotion
weborb
ajaxpro
hardware-infrastructure

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