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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:06:19+00:00 2026-05-23T23:06:19+00:00

Scenario: When I photograph an object, I take multiple images, from several angles. Multiplied

  • 0

Scenario: When I photograph an object, I take multiple images, from several angles. Multiplied by the number of objects I “shoot”, I can generate a large number of images. Problem: Camera generates images identified as, ‘DSCN100001’, ‘DSCN100002″, etc. Cryptic.

I put together a script that will prompt for directory specification (Windows), as well as a “Prefix”. The script reads the file’s creation date and time, and rename the file accordingly. The prefix will be added to the front of the file name. So, ‘DSCN100002.jpg’ can become “FatMonkey 20110721 17:51:02”. The time detail is important to me for chronology.

The script follows. Please tell me whether it is Pythonic, whether or not it is poorly written and, of course, whether there is a cleaner – more efficient way of doing this. Thank you.

   import os
   import datetime
   target = raw_input('Enter full directory path: ')
   prefix = raw_input('Enter prefix: ')
   os.chdir(target)
   allfiles = os.listdir(target)
   for filename in allfiles:
        t = os.path.getmtime(filename)
        v = datetime.datetime.fromtimestamp(t)
        x = v.strftime('%Y%m%d-%H%M%S')
        os.rename(filename, prefix + x +".jpg")
  • 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-23T23:06:20+00:00Added an answer on May 23, 2026 at 11:06 pm

    The way you’re doing it looks Pythonic. A few alternatives (not necessarily suggestions):

    You could skip os.chdir(target) and do os.path.join(target, filename) in the loop.

    You could do strftime('{0}-%Y-%m-%d-%H:%M:%S.jpg'.format(prefix)) to avoid string concatenation. This is the only one I’d reccomend.

    You could reuse a variable name like temp_date instead of t, v, and x. This would be OK.

    You could skip storing temporary variables and just do:

    for filename in os.listdir(target):
        os.rename(filename, datetime.fromtimestamp(
                             os.path.getmtime(filename)).strftime(
                              '{0}-%Y-%m-%d-%H:%M:%S.jpeg'.format(prefix)))
    

    You could generalize your function to work for recursive directories by using os.walk().

    You could detect the file extension of files so it would be correct not just for .jpegs.

    You could make sure you only renamed files of the form DSCN1#####.jpeg

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

Sidebar

Related Questions

Scenario: A stored procedure receives from code a DateTime with, let's say DateTime.Now value,
Scenario You've got several bug reports all showing the same problem. They're all cryptic
Scenario: There are multiple C structs, each of which contains a function pointer to
Scenario: I have imported the data from SQl Server to HDFS. The data stored
Scenario For a multiple file uploader I am implementing, I need to have a
Scenario: A web site with x number of pages is being served with a
Scenario Downloading an xlsx file to my local drive from a http location using
Scenario: Repository structure changed from /trunk/ to /projectName/trunk/ and the local copy is outdated
Scenario: I have several services that I want to be discovered by different clients.
Scenario: You have an ASP.Net webpage that should display the next image in a

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.