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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:32:55+00:00 2026-06-09T14:32:55+00:00

Using python with Windows Im trying to rename several files at once that are

  • 0

Using python with Windows Im trying to rename several files at once that are in the same folder but I cant use a list to do a rename that is why I get this error when I try my code:

os.rename(dirlist[1], words[1]) WindowsError: [Error 2] The system
cannot find the file specified

Here is the sample code:

import os
import sys
words = os.listdir('C:/Users/Any/Desktop/test')
dirlist = os.listdir('C:/Users/Any/Desktop/test')

words = [w.replace('E', 'e') for w in words]
print words 

os.rename(dirlist[1], words[1])

What I am trying to achieve is have my python script ran on a folder of choice and the script will take all the files in there and will rename all of them. But the tricky part comes when I cant single out the folder names and have them renamed because they are attached to the list.

  • 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-09T14:32:56+00:00Added an answer on June 9, 2026 at 2:32 pm

    os.listdir is only giving you back the basename results. Not full path. They don’t exist in your current working directory. You would need to join them back with the root:

    root = 'C:/Users/Any/Desktop/test'
    for item in os.listdir(root):
        fullpath = os.path.join(root, item)
        os.rename(fullpath, fullpath.replace('E', 'e'))
    

    Update

    In response to your comment about how to perform larger number of replacements, I had suggested you could use translate and maketrans.

    Let’s start with our dict and a source string:

    d = {'E': 'e', 'a': 'B', 'v': 'C'}
    s = 'aAaAvVvVeEeE'
    

    First, let me show you an example of a very primitive and entry level approach:

    for old, new in d.iteritems():
        s = s.replace(old, new)
    
    print s
    # BABACVCVeeee
    

    That example loops over your dictionary, calling the replacement multiple times. It works, and it makes perfect sense, using simple syntax. But it kind of sucks having to loop over the dictionary for every string and call replace multiple times.

    There are many other ways to do this I am sure, but another approach is to create a translation table once, and then reuse it for every string:

    import string
    
    old, new = zip(*d.items())
    print old, new
    # ('a', 'E', 'v') ('B', 'e', 'C')
    
    old_str, new_str = ''.join(old), ''.join(new)
    print old_str, new_str
    # aEv BeC
    
    table = string.maketrans(old_str, new_str)
    
    print s.translate(table)
    # BABACVCVeeee
    

    That will split the dictionary out to key and value tuples. Then we join then intro strings and pass them to maketrans, which will give us back a table. We only have to do that once. Now we have a table and can use it to translate any string.

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

Sidebar

Related Questions

How can I write to files using Python (on Windows) and use the Unix
I'm trying to start an application using Python. I've seen that some people use
I'm trying to extract files from a zip file using Python 2.7.1 (on Windows,
I'm using Python 2.7.3 on Windows XP via the IDLE GUI, and I'm trying
I want to write text files with DOS/Windows line endings '\r\n' using python running
I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7
I'm trying to copy files inside a Python script using the following code: inf,outf
I am using Python 2.5 in Windows and I am trying import curses.ascii.* to
I'm using python 3.2.2 on windows 7 and I'm trying to create a program
Using python 3.2, I am trying to decode bytes using str(bytes, cp1251) but 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.