Return newest files for each folder in a directory tree
I have several text files located in different folders.
Southwest (folder)
Texas (folder)
Houston (folder)
11-18-2018 (folder)
Houston.txt (date modified 11-18-2018)
11-11-2018 (folder)
Houston.txt (date modified 11-11-2018)
Austin (folder)
11-18-2018 (folder)
Austin.txt (date modified 11-18-2018)
11-11-2018 (folder)
Austin.txt (date modified 11-11-2018)
Southern_Pacific (folder)
California
San-Diego (folder)
11-18-2018 (folder)
San_Diego.txt (date modified 11-18-2018)
11-11-2018 (folder)
San_Diego.txt (date modified 11-11-2018)
Los_Angeles (folder)
11-18-2018 (folder)
Los_Angeles.txt (date modified 11-18-2018)
11-11-2018 (folder)
Los_Angeles.txt (date modified 11-11-2018)
and so on with other different regions in US.
Goal: I need to grab all the newest files for each folder based on the cities they reside. So the return value would be something like:
C:SouthwestTexasHouston11-18-2018Houston.txt
C:SouthwestTexasAustin11-18-2018Austin.txt
C:Southern_PacificCaliforniaSan_Diego11-18-2018San_Diego.txt
C:Southern_PacificCaliforniaLos_Angeles11-18-2018Los_Angeles.txt
I will then use the return values as path to open the files and feed through another function that I have built. But at the moment the other function only works when the files reside in the same folder where I run the script or I specifically point it to sub folder - not the entire tree.
So at this point I need to be able to go through each folder, grab the newest file for each city, return the value as path to the file.
I'm new to python or scripting in general. A helping hand would be greatly appreciated!
python
add a comment |
I have several text files located in different folders.
Southwest (folder)
Texas (folder)
Houston (folder)
11-18-2018 (folder)
Houston.txt (date modified 11-18-2018)
11-11-2018 (folder)
Houston.txt (date modified 11-11-2018)
Austin (folder)
11-18-2018 (folder)
Austin.txt (date modified 11-18-2018)
11-11-2018 (folder)
Austin.txt (date modified 11-11-2018)
Southern_Pacific (folder)
California
San-Diego (folder)
11-18-2018 (folder)
San_Diego.txt (date modified 11-18-2018)
11-11-2018 (folder)
San_Diego.txt (date modified 11-11-2018)
Los_Angeles (folder)
11-18-2018 (folder)
Los_Angeles.txt (date modified 11-18-2018)
11-11-2018 (folder)
Los_Angeles.txt (date modified 11-11-2018)
and so on with other different regions in US.
Goal: I need to grab all the newest files for each folder based on the cities they reside. So the return value would be something like:
C:SouthwestTexasHouston11-18-2018Houston.txt
C:SouthwestTexasAustin11-18-2018Austin.txt
C:Southern_PacificCaliforniaSan_Diego11-18-2018San_Diego.txt
C:Southern_PacificCaliforniaLos_Angeles11-18-2018Los_Angeles.txt
I will then use the return values as path to open the files and feed through another function that I have built. But at the moment the other function only works when the files reside in the same folder where I run the script or I specifically point it to sub folder - not the entire tree.
So at this point I need to be able to go through each folder, grab the newest file for each city, return the value as path to the file.
I'm new to python or scripting in general. A helping hand would be greatly appreciated!
python
1
First look at the os.walk() method.
– kantal
Nov 20 at 15:57
Is it always true, that the folder name is the date of modification? If so and you could change its naming format from "Month-Day-Year" to "Year-Month-Day", then the last folder in alphabetical order is always the newest.
– Sven Krüger
Nov 20 at 16:19
add a comment |
I have several text files located in different folders.
Southwest (folder)
Texas (folder)
Houston (folder)
11-18-2018 (folder)
Houston.txt (date modified 11-18-2018)
11-11-2018 (folder)
Houston.txt (date modified 11-11-2018)
Austin (folder)
11-18-2018 (folder)
Austin.txt (date modified 11-18-2018)
11-11-2018 (folder)
Austin.txt (date modified 11-11-2018)
Southern_Pacific (folder)
California
San-Diego (folder)
11-18-2018 (folder)
San_Diego.txt (date modified 11-18-2018)
11-11-2018 (folder)
San_Diego.txt (date modified 11-11-2018)
Los_Angeles (folder)
11-18-2018 (folder)
Los_Angeles.txt (date modified 11-18-2018)
11-11-2018 (folder)
Los_Angeles.txt (date modified 11-11-2018)
and so on with other different regions in US.
Goal: I need to grab all the newest files for each folder based on the cities they reside. So the return value would be something like:
C:SouthwestTexasHouston11-18-2018Houston.txt
C:SouthwestTexasAustin11-18-2018Austin.txt
C:Southern_PacificCaliforniaSan_Diego11-18-2018San_Diego.txt
C:Southern_PacificCaliforniaLos_Angeles11-18-2018Los_Angeles.txt
I will then use the return values as path to open the files and feed through another function that I have built. But at the moment the other function only works when the files reside in the same folder where I run the script or I specifically point it to sub folder - not the entire tree.
So at this point I need to be able to go through each folder, grab the newest file for each city, return the value as path to the file.
I'm new to python or scripting in general. A helping hand would be greatly appreciated!
python
I have several text files located in different folders.
Southwest (folder)
Texas (folder)
Houston (folder)
11-18-2018 (folder)
Houston.txt (date modified 11-18-2018)
11-11-2018 (folder)
Houston.txt (date modified 11-11-2018)
Austin (folder)
11-18-2018 (folder)
Austin.txt (date modified 11-18-2018)
11-11-2018 (folder)
Austin.txt (date modified 11-11-2018)
Southern_Pacific (folder)
California
San-Diego (folder)
11-18-2018 (folder)
San_Diego.txt (date modified 11-18-2018)
11-11-2018 (folder)
San_Diego.txt (date modified 11-11-2018)
Los_Angeles (folder)
11-18-2018 (folder)
Los_Angeles.txt (date modified 11-18-2018)
11-11-2018 (folder)
Los_Angeles.txt (date modified 11-11-2018)
and so on with other different regions in US.
Goal: I need to grab all the newest files for each folder based on the cities they reside. So the return value would be something like:
C:SouthwestTexasHouston11-18-2018Houston.txt
C:SouthwestTexasAustin11-18-2018Austin.txt
C:Southern_PacificCaliforniaSan_Diego11-18-2018San_Diego.txt
C:Southern_PacificCaliforniaLos_Angeles11-18-2018Los_Angeles.txt
I will then use the return values as path to open the files and feed through another function that I have built. But at the moment the other function only works when the files reside in the same folder where I run the script or I specifically point it to sub folder - not the entire tree.
So at this point I need to be able to go through each folder, grab the newest file for each city, return the value as path to the file.
I'm new to python or scripting in general. A helping hand would be greatly appreciated!
python
python
asked Nov 20 at 15:48
vmx1987
285
285
1
First look at the os.walk() method.
– kantal
Nov 20 at 15:57
Is it always true, that the folder name is the date of modification? If so and you could change its naming format from "Month-Day-Year" to "Year-Month-Day", then the last folder in alphabetical order is always the newest.
– Sven Krüger
Nov 20 at 16:19
add a comment |
1
First look at the os.walk() method.
– kantal
Nov 20 at 15:57
Is it always true, that the folder name is the date of modification? If so and you could change its naming format from "Month-Day-Year" to "Year-Month-Day", then the last folder in alphabetical order is always the newest.
– Sven Krüger
Nov 20 at 16:19
1
1
First look at the os.walk() method.
– kantal
Nov 20 at 15:57
First look at the os.walk() method.
– kantal
Nov 20 at 15:57
Is it always true, that the folder name is the date of modification? If so and you could change its naming format from "Month-Day-Year" to "Year-Month-Day", then the last folder in alphabetical order is always the newest.
– Sven Krüger
Nov 20 at 16:19
Is it always true, that the folder name is the date of modification? If so and you could change its naming format from "Month-Day-Year" to "Year-Month-Day", then the last folder in alphabetical order is always the newest.
– Sven Krüger
Nov 20 at 16:19
add a comment |
2 Answers
2
active
oldest
votes
here is my solution for you:
import os
from datetime import datetime
def getNewestFiles(startpath):
path = ""
currTime = 0
maxTime = 0
resultPath = ""
for root, dirs, files in os.walk(startpath): #search from start to all folders and files
maxTime = 0
resultPath = ""
for f in files:
path = root+"\"+f
currTime = os.path.getmtime(path) #get time of file
if(currTime > maxTime): #compare time with other files
maxTime = currTime
resultPath = path #take the largest number
if(resultPath != ""):
print('{} : {}'.format(resultPath, datetime.fromtimestamp(float(os.path.getmtime(resultPath))).strftime('%Y-%m-%d %H:%M:%S')))
You have to choose your startpath and then you get all newest files in all directories
This one seems to work as intended. Thank you!
– vmx1987
Nov 20 at 20:45
add a comment |
Use os.walk to navigate the directory structure and os.path.getmtime to determine the modification time for each file.
For comparing the times, I recommend using the datetime module.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396680%2freturn-newest-files-for-each-folder-in-a-directory-tree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
here is my solution for you:
import os
from datetime import datetime
def getNewestFiles(startpath):
path = ""
currTime = 0
maxTime = 0
resultPath = ""
for root, dirs, files in os.walk(startpath): #search from start to all folders and files
maxTime = 0
resultPath = ""
for f in files:
path = root+"\"+f
currTime = os.path.getmtime(path) #get time of file
if(currTime > maxTime): #compare time with other files
maxTime = currTime
resultPath = path #take the largest number
if(resultPath != ""):
print('{} : {}'.format(resultPath, datetime.fromtimestamp(float(os.path.getmtime(resultPath))).strftime('%Y-%m-%d %H:%M:%S')))
You have to choose your startpath and then you get all newest files in all directories
This one seems to work as intended. Thank you!
– vmx1987
Nov 20 at 20:45
add a comment |
here is my solution for you:
import os
from datetime import datetime
def getNewestFiles(startpath):
path = ""
currTime = 0
maxTime = 0
resultPath = ""
for root, dirs, files in os.walk(startpath): #search from start to all folders and files
maxTime = 0
resultPath = ""
for f in files:
path = root+"\"+f
currTime = os.path.getmtime(path) #get time of file
if(currTime > maxTime): #compare time with other files
maxTime = currTime
resultPath = path #take the largest number
if(resultPath != ""):
print('{} : {}'.format(resultPath, datetime.fromtimestamp(float(os.path.getmtime(resultPath))).strftime('%Y-%m-%d %H:%M:%S')))
You have to choose your startpath and then you get all newest files in all directories
This one seems to work as intended. Thank you!
– vmx1987
Nov 20 at 20:45
add a comment |
here is my solution for you:
import os
from datetime import datetime
def getNewestFiles(startpath):
path = ""
currTime = 0
maxTime = 0
resultPath = ""
for root, dirs, files in os.walk(startpath): #search from start to all folders and files
maxTime = 0
resultPath = ""
for f in files:
path = root+"\"+f
currTime = os.path.getmtime(path) #get time of file
if(currTime > maxTime): #compare time with other files
maxTime = currTime
resultPath = path #take the largest number
if(resultPath != ""):
print('{} : {}'.format(resultPath, datetime.fromtimestamp(float(os.path.getmtime(resultPath))).strftime('%Y-%m-%d %H:%M:%S')))
You have to choose your startpath and then you get all newest files in all directories
here is my solution for you:
import os
from datetime import datetime
def getNewestFiles(startpath):
path = ""
currTime = 0
maxTime = 0
resultPath = ""
for root, dirs, files in os.walk(startpath): #search from start to all folders and files
maxTime = 0
resultPath = ""
for f in files:
path = root+"\"+f
currTime = os.path.getmtime(path) #get time of file
if(currTime > maxTime): #compare time with other files
maxTime = currTime
resultPath = path #take the largest number
if(resultPath != ""):
print('{} : {}'.format(resultPath, datetime.fromtimestamp(float(os.path.getmtime(resultPath))).strftime('%Y-%m-%d %H:%M:%S')))
You have to choose your startpath and then you get all newest files in all directories
answered Nov 20 at 17:20
nerd100
875
875
This one seems to work as intended. Thank you!
– vmx1987
Nov 20 at 20:45
add a comment |
This one seems to work as intended. Thank you!
– vmx1987
Nov 20 at 20:45
This one seems to work as intended. Thank you!
– vmx1987
Nov 20 at 20:45
This one seems to work as intended. Thank you!
– vmx1987
Nov 20 at 20:45
add a comment |
Use os.walk to navigate the directory structure and os.path.getmtime to determine the modification time for each file.
For comparing the times, I recommend using the datetime module.
add a comment |
Use os.walk to navigate the directory structure and os.path.getmtime to determine the modification time for each file.
For comparing the times, I recommend using the datetime module.
add a comment |
Use os.walk to navigate the directory structure and os.path.getmtime to determine the modification time for each file.
For comparing the times, I recommend using the datetime module.
Use os.walk to navigate the directory structure and os.path.getmtime to determine the modification time for each file.
For comparing the times, I recommend using the datetime module.
answered Nov 20 at 16:05
John
1255
1255
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396680%2freturn-newest-files-for-each-folder-in-a-directory-tree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
First look at the os.walk() method.
– kantal
Nov 20 at 15:57
Is it always true, that the folder name is the date of modification? If so and you could change its naming format from "Month-Day-Year" to "Year-Month-Day", then the last folder in alphabetical order is always the newest.
– Sven Krüger
Nov 20 at 16:19