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

  • Home
  • SEARCH
  • 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 6530355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:44:43+00:00 2026-05-25T09:44:43+00:00

this code is meant to ask for a directory, then list al files in

  • 0

this code is meant to ask for a directory, then list al files in that directory, then rename then to their position in that list, problem is I always get error 2, file not found, while if i print the list it apperently does find the files because the list is not blank.

import os, sys    
path = input("input path: ")    
dirs = os.listdir(path)    
for i in range(0,len(dirs)):    
    os.rename(dirs[i], str(i))

Given input files, I want to rename the base file name with a number, but preserving the file extension. Thus

Input
‘a.txt’, ‘test.txt’, ‘test1.txt’

Output
‘0.txt’, ‘1.txt’, ‘2.txt’

  • 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-25T09:44:44+00:00Added an answer on May 25, 2026 at 9:44 am

    Yup, so you do need to add the code from my comment. The problem is os.listdir is only returning base file names so when the rename is called, it expects to find those files in whatever directory Python thinks it should be in. By adding the os.path.join, it will build out the fully qualified path to the file so the rename will work correctly.

    In the comments, OP stated the files got moved up a folder which lead me to believe the rename needed a fully qualified path on second argument. Also, we learned the files should not be renamed from foo.txt to 0 but instead should become 0.txt etc (preserving file extension) This code now

    import os, sys
    path = input("input path: ")
    dirs = os.listdir(path)
    for i in range(0,len(dirs)):
       # capture the fully qualified path for the original file
       original_file = os.path.join(path, dirs[i])
       # Build the new file name as number . file extension
       # if there is no . in the file name, this code goes boom
       new_file = os.path.join(path, str(i) + '.' + original_file.split('.')[-1])
    
       print "Renaming {0} as {1}".format(original_file, new_file)
       os.rename(original_file, new_file)
    

    Verified with Python 2.6.1

    Showing the relevant bits from the command line. You can see the empty files bar.txt and foo.txt are renamed to 0 and 1

    >>> path = input("Input path")
    Input path"/Users/bfellows2/so"
    >>> dirs = os.listdir(path)
    >>> dirs
    ['bar.txt', 'foo.txt']
    >>> for i in range(0,len(dirs)): 
    ...  os.rename(os.path.join(path, dirs[i]), str(i))
    ... 
    >>> 
    [1]+  Stopped                 python
    Helcaraxe:so bfellows2$ ls -al
    total 0
    drwxr-xr-x    4 bfellows2  bfellows2   136 Sep  3 20:30 .
    drwxr-xr-x  100 bfellows2  bfellows2  3400 Sep  3 20:24 ..
    -rw-r--r--    1 bfellows2  bfellows2     0 Sep  3 20:24 0
    -rw-r--r--    1 bfellows2  bfellows2     0 Sep  3 20:24 1
    Helcaraxe:so bfellows2$ python -V
    Python 2.6.1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no idea what this means. But here is the code that it
hi I would like to ask how I would modify this code for the
Disclaimer: This question is meant to be purely theoretical, so please don't ask me
I recently came across a problem with my code that was caused by certain
Could someone explain to me what this portion of code means? repeat scroll 0
What does nonatomic mean in this code? @property(nonatomic, retain) UITextField *theUsersName; What is the
Does anyone know why this code doesn't work. This means, the alert is NOT
I got this code from the wordpress <head profile=http://gmpg.org/xfn/11> What does this means? what
I'm asking more about what this means to my code. I understand the concepts
Does anyone know what this error message means? FATAL: Code generation error detected during

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.