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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:15:28+00:00 2026-05-25T16:15:28+00:00

I am writing a simple Python script to rename all files in a directory

  • 0

I am writing a simple Python script to rename all files in a directory to replace all spaces in the file name with hyphens. I have the following which is crashing on os.rename

import os

path =  os.getcwd()
filenames = os.listdir(path)

for filename in filenames:
    os.rename(os.path.join(path + filename), os.path.join(path + filename.replace(" ", "-")))

Gives the error in the console:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
OSError: [Errno 2] No such file or directory

Any ideas on why this is happening?

  • 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-25T16:15:29+00:00Added an answer on May 25, 2026 at 4:15 pm

    I think it’s just because you have the syntax wrong in your call to os.path.join, the items you’re joining should be supplied as two distinct arguments, separated by a comma. This works fine for me:

    Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> path  = os.getcwd()
    >>> filenames = os.listdir(path)
    >>> for filename in filenames:
    ...     os.rename(os.path.join(path, filename), os.path.join(path, filename.replace(' ', '-')))
    ...
    >>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple methods for writing a python object to a file
I am writing a simple Python script with no GUI. I want to be
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
I am writing a python script which looks at common computer files and examines
I was writing a simple python script to read from and reconstruct data from
I'm writing a simple Python CGI script that grabs a webpage and displays the
I will be writing a little Python script tomorrow, to retrieve all the data
I'm writing a script to convert very simple function documentation to XML in python.
I'm interested in writing a short python script which uploads a short binary file
I have simple Python script to execute test suite both under Windows and Linux.

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.