5 Replies - 1970 Views - Last Post: 29 November 2011 - 08:07 AM Rate Topic: -----

#1 cupidvogel   User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Copyfile error

Posted 23 November 2011 - 02:06 AM

Hi, I am trying to copy the contents of one text file into another via shutil.copyfile. However it is throwing error (shown in the pic), both when the destination text file already exists, and doesn't exist. Any help???

Attached image(s)

  • Attached Image

This post has been edited by cupidvogel: 23 November 2011 - 02:10 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Copyfile error

#2 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 323
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Copyfile error

Posted 24 November 2011 - 06:58 PM

It seems to be throwing an IOError, which means either it can't read the file, or the directory you are copying to isn't writable. Is there any reason you are using copyfile instead of copy? I'd have a look at the documentation.

When working with files it's usually a good idea to make them OS independant, and use os.join() - there is a good example of using shutil.copy, Here

This post has been edited by Simown: 24 November 2011 - 06:58 PM

Was This Post Helpful? 1
  • +
  • -

#3 cupidvogel   User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Copyfile error

Posted 25 November 2011 - 10:41 AM

shutil.copy is used to copy a file from one location to another location. This is not what I am trying to do here, I am trying to copy the contents of one text file into another's. I am using Windows, and here all other file related commands are working fine, so I don't think that separator is an issue. Both directories exist, I guarantee!
Was This Post Helpful? 0
  • +
  • -

#4 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 323
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Copyfile error

Posted 25 November 2011 - 11:02 AM

I haven't got the means to test it right now, but I will when I get home. Also, do you have to use shutil.copy, or can you use a different python method to achieve it? Reading and writing to a file object would be one way around it.

This post has been edited by Simown: 25 November 2011 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 cupidvogel   User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Copyfile error

Posted 25 November 2011 - 11:20 AM

May be there is another workaround. But I want to know why it isn't working.
Was This Post Helpful? 0
  • +
  • -

#6 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Copyfile error

Posted 29 November 2011 - 08:07 AM

If you look at the error message, you will see that all directories are double-backslashed up until the final filename:
"C:\\Documents and Settings\\Kausuv\\Desktop\targ.xt"



My guess is that it should be:
"C:\\Documents and Settings\\Kausuv\\Desktop\\targ.xt"



But it is hard to tell when you can't see the code.
Was This Post Helpful? 1
  • +
  • -

#7 cupidvogel   User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Copyfile error

Posted 01 December 2011 - 07:23 AM

Yeah yeah, that was indeed the culprit. Strange to see it escaped both me and Simown...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1