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

Running a script for many files of the same extension. Getting 'UnboundLocalError'
Connecting to IBM AS400 server for database operations hangs
scikit learn: polynomial interpolation of higher dimensions
Centralized Django Installations with VirtualEnv
Matplotlib - Draw points that satisfy condition
Lektor Pagination - TemplateSyntaxError: Encountered unknown tag 'endblock'
Can anyone help me out with the ASCII part please
Compare two databases for any differences
string to float error
python3 sending serial data to Nextion Display
Probablistic graphical model Error while fitting the model
Unable to unzip using python zipfile module
Sqlite python - attempt to write a read only database
How to choose or assign variable in django template?
How can I return to the top of the script in python?
Python Multiprocessing Process seems to stop before doing anything

Categories

HOME
visual-studio-2015
ajax
ember.js
openssl
websphere
xamarin.android
facebook-graph-api
vs-team-services
thunderbird-addon
h2
propertygrid
defragmentation
algorithmic-trading
simpy
ng-admin
onenote-api
vuex
jboss-eap-7
image-recognition
cratedb
rebol2
datagrip
bar-chart
scenebuilder
phpbb
riak-ts
alfresco-share
google-openid
boolean-expression
sonarqube-msbuild-runner
rails-activerecord
go-cd
csh
maximo
web-frontend
google-api-dotnet-client
multichoiceitems
aurelia-binding
forum
piecewise
smart-mobile-studio
pyscripter
von-neumann
viewstate
google-guava-cache
mmdrawercontroller
bing-maps-api
office365connectors
poco-libraries
haskell-pipes
wcf-security
pdftk
z3py
polar-coordinates
master
c64
backstop.js
xib
linkageerror
jackson-databind
string-parsing
mpeg-4
git-rebase
approval-tests
license-key
eyeql
wikitext
selendroid
kendo-menu
android-snackbar
apigee-baas
built-in
prezto
ytplayerview
sqoop2
utf-32
cg
node-imagemagick
genetic-programming
funq
outline
surrogate-key
sgml
magic-numbers
reserved-words
swrl
nuspec
grunt-contrib-compass
qtgui
prng
moq-3
cadisplaylink
unions
makefile-project
v4l
viewdidload
path-manipulation
throttling
serp
server-error
evb
standardized

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