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

Converting Nested Json into Python object
pytest: Is test performance faster for tests in many small modules or wrapped under classes in a few large modules?
How to get more then 100,000 results in respond using Google BigQuery python API?
grequests - first couple of responses 200 then 403
Defining a semantic orientation for a small dataset
Python replace a line only if it meets a condition [duplicate]
Retrieving Excel cell names with XLRD [duplicate]
Install pyinstaller with no internet access
Confused about type hints in Python3
Append() applied to a list goes into loop
MultiIndex from Pivot Tuple Index
Multiclient server in Python - how to broadcast?
PyCharm doesn't appear to run all unit tests
Solve system of linear integer equations in Python
Django migration introducing unique_together
CQLSH cannot connect - TypeError('ref() does not take keyword arguments' [duplicate]

Categories

HOME
arrays
sidekiq
tinyos
dynamics-crm
jar
rotation
angular2-routing
reportportal
d
thunderbird-addon
memory-leaks
google-sheets-api
docker-swarm
bibtex
apple-numbers
iis-7.5
filter
bro
websphere-liberty
google-spreadsheet-api
unity2d
ctypes
data-analysis
maxima
bootstrap-popover
scenebuilder
jpeg2000
plupload
multiple-columns
shippo
richfaces
rworldmap
logarithm
net-snmp
cep
xilinx-ise
forum
autofill
clockwork
picturebox
rhino
large-data
openshift-enterprise
pace
ksoap
broadleaf-commerce
forecasting
topic-modeling
freetts
linq-to-entities
soundjs
android-maps-v2
html-agility-pack
prototypejs
context-free-language
openweathermap
tableau-server
spring-data-hadoop
cgcontext
fontconfig
launch
createobject
project-template
tomee
software-product-lines
brython
codesign
xmp
throughput
registrykey
harp
rspec3
callstack
mdm-zinc
custom-url
musl
client-side-validation
grunt-contrib-concat
firebug-lite
code-testing
funkload
visualstatemanager
opensocial
bigint
cdata
osx-snow-leopard
loop-invariant
sslexception
execvp
html-form-post
carddav
qtgui
device-emulation
gdataxml
marmalade-edk
cadisplaylink
getusermedia
continuous-testing
path-manipulation
querystringparameter
posting
paintcomponent
out-of-browser
jquery-ui-button
chdatastructures
revision
rd

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