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

How to insert into sqlite faster [duplicate]
How to read and extract and merge multiple huge size csv(1G~)?
how to extract data from json. other answers here did not work for me
How to execute a script or a funciton through python setuptoos?
jinja2 link to static files
multiple checkbox sqlalchemy delete
Pycharm Quick Documentation: Fetching Documentation
How can I create binary label from two tables
pyautogui.locateOnScreen() Returns… Nothing?
How can I judge whether a mail send sccess or not use Python?
Django Admin list_display product list
Python: How can I index in MapReduce(MRJob)?
Download multiple CSV files from a list in a single CSV (Python)
Does Python garbage collect when Heroku warns about memory quota vastly exceeded (R15)?
reading function from another file into a button in python
Python - Mult-Threading Help - Reading Multiple Files - ETL Into SQL Server

Categories

HOME
cluster-computing
filterrific
jsviews
d
mapbox-gl-js
ubuntu-12.04
phpstorm
apache2
wampserver
javamail
jrebel
sequelize.js
hosts
xamarin-studio
iso
android-source
game-maker-studio-1.4
xul
off-canvas-menu
rails-activerecord
multiplayer
delete-file
rhmap
bus-error
maximo
i3
hana-studio
configure
websauna
pep8-assembly
referenceerror
protobuf-net
data-extraction
ocs
ksoap
mms
inkscape
soundjs
android-preferences
intellij-idea-2016
exiftool
nsexception
control-flow-graph
gist
jexl
mcrypt
addin-express
push-diffusion
scalar
search-regex
qt-linguist
data-import
hornetq
achartengine
team-build
leadtools-sdk
sysfs
cpan
start-job
c++-actor-framework
inputaccessoryview
cloudpebble
node-imagemagick
mov
yahoo-boss-api
boost-test
wss
zipalign
reserved-words
xenocode
circos
healthvault
trimming
sttwitter
haskell-platform
cbcentralmanager
pitch
enumerators
cosm
subtract
maven-ant-tasks
selectonemenu
hashalgorithm
opengl-es-lighting
smooth
search-path
drawtext

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