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

builtins.TypeError: __init__() missing 2 required positional arguments: 'height' and 'radius'
How to do a reverse operation to tf.nn.embedding_lookup?
django complex query for interdependant tables
Is there a more efficient and elegant way to filter pandas index by date?
How do I temporarily use Python 3.6 in terminal Mac?
Difference between pre-trained word embedding and training word embedding in keras
AttributeError: module 'xlwings' has no attribute 'save'
Images broadcast over UDP socket (Python)
Index class as list and as dictionary
Python “will the arrow fly straight program”
Processing an eventlog with Pandas - find next match in DataFrame
Intellij python plugin debugging the script copied under the target directory
Create a list with repeated values with list comprehension
Is LASSO regression implemented in Statsmodels?
There is a duplicate line showed when calling a __init__ in Python script
trouble with mousewheel + scrollbars in tkinter

Categories

HOME
symfony
voip
performancecounter
bower
paramiko
reportportal
wms
angular2-directives
checksum
httpclient
eval
goutte
diagram
facebook-javascript-sdk
cockroachdb
mongodb-query
pdo
alfresco-share
ms-access-2007
jogl
apply
fop
hhvm
badge
pipelinedb
image-quality
vcard
winscp
pygooglechart
gpib
edb
pdflatex
shapes
hawtio
honeysql
sidr
dart-pub
intersystems-ensemble
jdbi
cppunit
python-idle
tuleap
jags
etherpad
fedora20
in-memory-database
spyne
efxclipse
coremidi
android-navigationview
windows-azure-pack
tomee
icefaces
spark-cassandra-connector
lemoon
drf-nested-routers
mysqldumpslow
sem
textkit
ipojo
on-duplicate-key
ytplayerview
ffprobe
apache-mina
mdm-zinc
ember-cli-addons
consensus
google-earth-plugin
moai
activity-streams
ogr2ogr
yahoo-boss-api
funkload
magic-numbers
crystal-reports-10
db4o
magicalrecord-2.2
fogbugz-api
asp.net-mvc-scaffolding
swrl
zend-lucene
trailing-slash
mvccontrib
linkedhashset
macruby
text-size
search-path
j-interop
unreachable-code
soft-keyboard

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