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

if there any better way to read bb function souce code.i was very faint
PyQt4 signals and slots
Sql Alchemy What is wrong?
How do I plot a graph in Python?
Reordering matrix elements to reflect column and row clustering in naiive python
making urllib request in Python from the client side
Grid within a frame?
how to create http headers from scratch
Mapping a range of values to another
maya2008 win32api 64 bit python
Python importing modules differently when run interactively/non-interactively
Why do I get this error in this Python code?
what is the correct way to close a socket in python 2.6?
Listing blog entries by year,month
Extract URLs out of email in Python
Packet Queue in Python?

Categories

HOME
sass
meshlab
boost-thread
laravel-5.3
qpython3
deep-linking
travis-ci
couchdb-2.0
azureservicebus
rebol
requirejs
subquery
unity2d
pjsip
game-physics
symfony-forms
uiview
eclipse-luna
session-timeout
internet-explorer-8
zoho
raphael
statusbar
apple-tv
url.action
bcrypt
myob
kong
bus-error
hash-collision
pep8-assembly
android-vpn-service
body-parser
fabric-digits
ios-ui-automation
gettext
threshold
nunit-3.0
jboss-esb
axis-labels
visualstudio.testtools
tripwire
double-buffering
data-integration
spring-data-hadoop
crash-reports
rpostgresql
pagefile
jszip
barcode-printing
build-process
cjson
angularjs-ng-pattern
asteriskami
clob
squirrel
string-parsing
faraday
project-online
amazon-kcl
yoothemes
handlebars.java
android-snackbar
abas
leadtools-sdk
xaml-designer
ember-cli-addons
consensus
cocoascript
voldemort
joox
config-files
late-static-binding
reserved-words
dllexport
ruboto
fileconveyor
qt4.6
bulbs
returnurl
plone-funnelweb
opengl-es-lighting
for-xml-path
ets
gethashcode
post-redirect-get
gwt-2.2-celltable
msf
associativity

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