Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:31:35+00:00 2026-05-23T17:31:35+00:00

I found this post on stackoverflow which was exactly what I wanted to integrate

  • 0

I found this post on stackoverflow which was exactly what I wanted to integrate into a larger script I am writing: Find the newest folder in a directory in Python

I want to check for the newest file or folder so modified the script for testing as follows:

#!/usr/bin/env python3.1

import os

def allFilesIn(b='.'):
    result = []
    for d in os.listdir(b):
        bd = os.path.join(b, d)
    result.append(bd)
    return result

latest_subdir = max(allFilesIn('/tmp/testforlatest'), key=os.path.getmtime)

print(latest_subdir)

However I get results as follows:

> touch /tmp/testforlatest/file1
> ls -t -1 /tmp/testforlatest/ | head -1
file1
> /tmp/testfornewestfile.py 
/tmp/testforlatest/file1
> touch /tmp/testforlatest/file2
> ls -t -1 /tmp/testforlatest/ | head -1
file2
> /tmp/testfornewestfile.py 
/tmp/testforlatest/file1
> mkdir /tmp/testforlatest/folder1
> ls -t -1 /tmp/testforlatest/ | head -1
folder1/
> /tmp/testfornewestfile.py 
/tmp/testforlatest/folder1
> mkdir /tmp/testforlatest/folder2
> ls -t -1 /tmp/testforlatest/ | head -1
folder2/
> /tmp/testfornewestfile.py 
/tmp/testforlatest/folder1
> touch /tmp/testforlatest/file3
> ls -t -1 /tmp/testforlatest/ | head -1
file3
> /tmp/testfornewestfile.py 
/tmp/testforlatest/folder1

Would someone mind explaining why this is happening and what I’m doing wrong.

other information which may be of use:

> python3.1 --version
Python 3.1.3
> cat /etc/debian_version 
6.0.2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:31:35+00:00Added an answer on May 23, 2026 at 5:31 pm

    Your function allFilesIn only returns the last file returned by os.listdir because you append the results outside of the for loop. You probably meant this:

    def allFilesIn(b='.'):
        result = []
        for d in os.listdir(b):
            bd = os.path.join(b, d)
            result.append(bd)
        return result
    

    As an aside it’s preferable to use lowercase and underscores for function names per PEP8. You could also condense the function into a list comprehension pretty easily:

    def all_files_in(path='.'):
        return [os.path.join(path, f) for f in os.listdir(path)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to use groovy for a little ftp script and found this post
I found this post: What's the Best Way to Shuffle an NSMutableArray? And as
I found this post on Crystal Reports Cutting Off Text in PDF , but
I found this post that shows how to pass multiple check box selections to
I have set a property across threads before and I found this post Cross-thread
I was reading through this thread: Hidden Features of JavaScript? and found this post:
I haven't found similiar post so I'm asking this. Let's say I defined somewhere
Accidentally I found this post about a new feature in ASP.NET 4.0: Expressions enclosed
This has probably been asked before but I can't find any relevant post using
This is my first post on stackoverflow.com so please be kind (rewind) ;) I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.