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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:02:22+00:00 2026-06-08T11:02:22+00:00

I have the following code to copy a section of a text file to

  • 0

I have the following code to copy a section of a text file to a new, temporary file. I am trying to create the temp file in the same directory as the file that is being copied. All of the print statements are to see how far it runs before crashing, pdb prints 1 and then give the error that is screencapped below the code.

def copymp(mptfile):
    print 1
    temp = os.path.dirname(mptfile) + '/mpdata.tmp'
    print 2
    mpfile = open(temp, 'w')
    print 3
    copyline = False
    for line in mptfile:
        print 4
        if line.startswith('MP'):
            copyline = True
            print 5
        if copyline:
            print 6
            print>>mpfile, line
            copyline = False
    mpfile.seek(1)
    return None

Error: https://i.stack.imgur.com/hDtTI.jpg

  • 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-08T11:02:23+00:00Added an answer on June 8, 2026 at 11:02 am

    The parameter to os.path.dirname() must be a string, but you are apparently passing in a file object. (Is this intended?) Try

    temp = os.path.dirname(mptfile.name) + '/mpdata.tmp'
    

    instead.

    (Side note: You should look closely at the traceback you get. The traceback shows in which line the error is occurring – no need for the print statements to isolate it. In this case, you can see it’s the call to os.path.dirname() that is failing, so you should check its documentation to diagnose the problem.)

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

Sidebar

Related Questions

I have the following code where I make an copy of my database that
I have the following block of jQuery code that I'm using to copy some
I have the following code for my .htaccess file that I've picked up from
I have the following code copy/pasted multiple times. The values that change are the
Hi i have the following code: public List<Person> findAll() { List<Person> copy = new
I have the following C++ code that I'm trying to get to compile (relevant
I have the following code: $file = 'http://3.bp.blogspot.com/-AGI4aY2SFaE/Tg8yoG3ijTI/AAAAAAAAA5k/nJB-mDhc8Ds/s400/rizal001.jpg'; $newfile = '/img/submitted/yoyo.jpg'; if ( copy($file,
i have the following piece of code, to copy files to my usb drive
I have something like the following code:: <xsl:variable name=sample> <xsl:copy-of select=//foo> <xsl:copy-of select=//bar> </xsl>
I have the following code to copy files sprintf(command, copy /Y %s %s, sourceFile,

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.