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

regex returns None on parsing address
Purpose of pyVim within pyVmomi
Formatting string to certain character limit in Python
Python Django return unique save dates and filter model objects
Moved time zones now datetime.utcnow() is incorrect
Pybind11 Type Error
Problems with the screen manager of Kivy Python
python selenium get asp-classic element
pip install requirements.txt issue when deploying Django app on Heroku
How to run a Python shell in azure app service
When does Python decode a byte string while reading a file?
Wordnet synset - strange list index out of range Error
python subprocess, No such file or directory
c mmap write long to mmaped-area
ModelForm has no model class specified when use model forms
python scipy load mat file error

Categories

HOME
sharepoint
caching
jdbc
mql4
winapi
jsviews
performancecounter
ubuntu-12.04
laravel-4
ll
openwrt
google-plus
constructor
diagram
eclipse-cdt
keystore
cortex-a
onsen-ui2
flann
session-timeout
derived
prestodb
ejbca
ip-camera
multiple-columns
scalaz7
apply
thingsboard
kong
mayavi
winscp
gzip
autofill
percentage
ios-ui-automation
reactivemongo
concrete5-5.7
fltk
linq-to-entities
common.logging
ical-dotnet
excel-interop
tango
ttcn
dojox.grid.datagrid
gd
pluck
etherpad
cdk
search-box
teiid
bettercms
festival
emgu
spyne
payu
hspec
tomee
picasa
ruby-2.0
json-schema-validator
instruments
metalsmith
database-backups
uwp-maps
date-format
selecteditem
post-processing
document-oriented-db
stream-framework
musl
start-job
livechat
ogr2ogr
java-collections-api
dache
winrt-httpclient
cling
angulartics
windows-mobile-6
image-zoom
threadx
argb
client-side-scripting
soa-suite
prng
arbor.js
gdataxml
phpsh
zend-framework-modules
ubuntu-11.10
mpmovieplayer
driving-directions
goliath
scalaxb
for-xml-path
adobe-contribute
post-redirect-get
chatroom
msn
revert
gwt-2.2-celltable
iphone-sdk-3.2
usability-testing





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android


MS Developer
developer works
python
ios
c
html
jquery


RDBMS discuss