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 8351239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:34:24+00:00 2026-06-09T08:34:24+00:00

I am trying to write code that will fetch the files in a directory

  • 0

I am trying to write code that will fetch the files in a directory that have been created/modified within a specific date range.

I do not know much about linux and I would like to know what command I can use to get a list of files in a directory that match within a date range I specify.

also, what is the correct formating for this type of query, as this process will be automated and the user needs to just put in his start and end dates.

the relevant code so far:

#! /usr/bin/env python

import os
import copy
import subprocess
import optparse

def command(command):
    env = copy.deepcopy(os.environ)
    proc = subprocess.Popen([command],
                shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result = proc.stdout.read()

if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option("-s", "--startdate", dest = "startdate",\
                      help = "the starting date of the files to search")
    parser.add_option("-e", "--enddate", dest = "enddate",\
                      help = "the ending date of the files to search")
    (options, args) = parser.parse_args()

    # commands
    file_names = command("get files that match dates command")

What must I put in that command to get these file names?

EDIT:

conversely – it does not have to be a command, if it can be done using pure code, such as os.walk for instance, that is also great. I know that certain features dont work exactly in Linux and Windows, so help on this matter would be warranted.

EDIT 2:

Regardless of the method, the user should input two dates: start and end. and then get all files that are modified/created between those dates.

  • 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-06-09T08:34:25+00:00Added an answer on June 9, 2026 at 8:34 am

    One option would be to use something in lines of os.walk and filter out files based on ctime/mtime, which you can get like this:

    import os.path, time
    print "last modified: %s" % time.ctime(os.path.getmtime(file))
    print "created: %s" % time.ctime(os.path.getctime(file))
    

    If you prefer to do it with shell, then find is your friend, with the following flags:

    -ctime n
    File’s status was last changed n*24 hours ago.

    -mtime n
    File’s data was last modified n*24 hours ago.

    [edit]

    A small code example to get modification time of files in a given dir (“.”):

    import os
    from os.path import join
    import datetime
    
    
    def modification_date(filename):
            t = os.path.getmtime(filename)
            return t
    
    def creation_date(filename):
            t = os.path.getctime(filename)
            return t
    
    for root, dirs, files in os.walk("."):
            for name in files:
            print join(root, name), modification_date(join(root, name)), creation_date(join(root, name))
    

    Depending on your particular commandline parameters implementation you want to convert what’s passed on commandline to a unix timestamp and compare with either of the dates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some code that will generate accurate .proto files from
I'm trying to write some Python code that will traverse each directory in the
I'm trying to write code that will load an image from a resource, and
I am trying to write some code that that will draw the line which
I am currently trying to write some code that will accept some FTP details,
I'm trying to write some Python code that will establish an invisible relay between
Hi I am trying to write an application that will play morse code. I
I'm trying to write code in my controller that when run, will create a
I've been trying to write code that loads a .png file, attaches hotspot information,
I'm trying to write some code that will take any object and convert it

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.