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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:46:11+00:00 2026-05-28T06:46:11+00:00

I have the function below that copies a file in a directory and recreate

  • 0

I have the function below that copies a file in a directory and recreate it in the directory where the function is called. When I am running the code part by part in ipython, it is working fine. However, when I execute it as a function it is giving me the following error:

---> 17     shutil.copy2(filein[0], os.path.join(dir,'template.in'))

TypeError: 'type' object is not subscriptable

Here is the function

import os
import shutil
from find import find

def recreatefiles(filedir):
    currdir = os.getcwd() # get current directory
    dirname = 'maindir'
    dir = os.path.join(currdir,dirname)
    if not os.path.exists(dir):
        os.makedirs(dir)

    #Copy .in files and create a template
    filein = find('*.in',filedir) # find is a function created

    shutil.copy2(filein[0], os.path.join(dir,'template.in'))

Any ideas about the error? Thanks

EDIT: Here is the code for find

import os, fnmatch
def find(pattern, path):
    result = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                if not name.startswith('.'):
                    result.append(os.path.join(root, name))
    return result

EDIT2: Output of filein from ipython

  [1]: filein
  [2]: ['/home/Projects/test.in']

Basically, there is just one file. I used filein[0] in shutil.copy2 to remove the square brackets

  • 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-28T06:46:11+00:00Added an answer on May 28, 2026 at 6:46 am

    I don’t see how you can possibly get 'type' object is not subscriptable with this code (as a matter of fact, I can successfully run it on my computer and have it copy one file).

    This suggests that the code you’re running isn’t the code that you think you’re running.

    I would do two things:

    1. make sure that the code is exactly as it appears in your question;
    2. since you appear to be running this in an interactive shell, make sure you close and restart ipython (to eliminate the possibility that you’re accidentally calling an older version of find() that got imported earlier).

    As a side note, I’d explicitly handle the case where filein is empty: the current code would raise an exception (list index out of range).

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

Sidebar

Related Questions

I have the function below, but the problem is that the zip file will
I have an example function below that reads in a date as a string
I have a function that resembles the one below. I'm not sure how to
I have a function below that I want to trigger only when a checkbox
I have a function below that is supposed to check if the array contains
I have a function that removes a file after a certain amount of time.
I have a PHP function below that is used for a command line tool
I have this function below that takes a list of id's and searches the
I have the function below that validates the return of another function (function searchPlaces
I have a JS function below that generates an href which includes the JQ

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.