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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:01:32+00:00 2026-06-14T16:01:32+00:00

I want to write a little script for managing a bunch of files I

  • 0

I want to write a little script for managing a bunch of files I got. Those files have complex and different name but they all contain a number somewhere in their name. I want to take that number, place it in front of the file name so they can be listed logically in my filesystem.

I got a list of all those files using os.listdir but I’m struggling to find a way to locate the numbers in those files. I’ve checked regular expression but I’m unsure if it’s the right way to do this!

example:

import os
files = os.litdir(c:\\folder)
files
['xyz3.txt' , '2xyz.txt', 'x1yz.txt']`

So basically, what I ultimately want is:

1xyz.txt
2xyz.txt
3xyz.txt

where I am stuck so far is to find those numbers (1,2,3) in the list files

  • 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-14T16:01:33+00:00Added an answer on June 14, 2026 at 4:01 pm

    This (untested) snippet should show the regexp approach. The search method of compiled patterns is used to look for the number. If found, the number is moved to the front of the file name.

    import os, re
    
    NUM_RE = re.compile(r'\d+')
    
    for name in os.listdir('.'):
        match = NUM_RE.search(name)
        if match is None or match.start() == 0:
            continue  # no number or number already at start
        newname = match.group(0) + name[:match.start()] + name[match.end():]
        print 'renaming', name, 'to', newname
        #os.rename(name, newname)
    

    If this code is used in production and not as homework assignment, a useful improvement would be to parse match.group(0) as an integer and format it to include a number of leading zeros. That way foo2.txt would become 02foo.txt and get sorted before 12bar.txt. Implementing this is left as an exercise to the reader.

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

Sidebar

Related Questions

Hallo, I have to write a little java script and have no idea how
I am currently trying to write a little powershell script (I have no experience
I have a submit form with method POST, I want to write a script
I'm trying to write a little build script, and want to determine if the
I want to write a little progress bar using a bash script. To generate
I want write a little code analyzer which parses nested structures and translates into
I want to write a little program which is able to work with Google
I want to write a batch script statement where: FINDSTR has to check for
I want to write a program to contact a PHP script online, and show
I have a little test.sh script that I run from the command line ./test.sh

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.