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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:22:23+00:00 2026-06-12T15:22:23+00:00

I have multiple folders that look like folder.0 and folder.1 . Inside each folder

  • 0

I have multiple folders that look like folder.0 and folder.1.
Inside each folder there is one file ('junk') that I want to copy and rename to the .0 or .1 part of the folder name in which it currently resides.
Here is what I’m trying to do:

inDirec = '/foobar'
outDirec = '/complete/foobar'


for root, dirs,files in os.walk(inDirec):
    for file in files:
        if file =='junk'
            d = os.path.split(root)[1]
            filename, iterator = os.path.splitext(d)  # folder no. captured
            os.rename(file, iterator+file) # change name to include folder no.
            fullpath = os.path.join(root,file)
            shutil.copy(fullpath,outDirec)

This returns:

os.rename(file,iterator+file)
OSError: [Errno 2] No such file or directory

I’m not even sure I should be using os.rename. I just want to pull out files == 'junk' and copy them to one directory but they all have the exact same name. So I really just need to rename them so they can exist in the same directory. Any help?

Update

    for root, dirs,files in os.walk(inDirec):
    for file in files:
        if file =='junk'
            d = os.path.split(root)[1]
            filename, iterator = os.path.splitext(d)  # folder no. captured
            it = iterator[-1] # iterator began with a '.'
         
            shutil.copy(os.path.join(root,file),os.path.join(outDirec,it+file))
  • 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-12T15:22:24+00:00Added an answer on June 12, 2026 at 3:22 pm

    Your problem is that your program is using the working directory at launch for your rename operation. You need to provide full relative or absolute paths as arguments to os.rename().

    Replace:

    os.rename(file, iterator+file)
    fullpath = os.path.join(root,file)
    shutil.copy(fullpath,outDirec)
    

    With (if you want to move):

    os.rename(os.path.join(root, file), os.path.join(outDirec, iterator+file))
    

    Or with (if you want to copy):

    shutil.copy(os.path.join(root, file), os.path.join(outDirec, iterator+file))
    

    NOTE: The destination directory should already exist or you will need code to create it.

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

Sidebar

Related Questions

Inside my main folder, I have multiple sub-folders and each sub folder contains multiple
I have a parent folder that contains inside it multiple folders named Project-* (folders
I have a folder with multiple sub-folders and each sub-folder contains 10-15 files. I
I have a folder with numerous files that I need to copy to multiple
I have an asp.net application containing pages that lie in multiple folder. I have
Is it preferred to have one depot with multiple folders which map to different
I have multiple websites set up in the same folder, and I want to
I would like to have multiple files in a folder with are managed by
I have a folder with multiple files, and I'd like to remove all <script>
I have a website with multiple folders that have different purposes on the site.

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.