Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61789 > unrolled thread

Need help with file object

Started byUnix SA <d.joshi84@gmail.com>
First post2013-12-13 09:59 +0530
Last post2013-12-13 05:37 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Need help with file object Unix SA <d.joshi84@gmail.com> - 2013-12-13 09:59 +0530
    Re: Need help with file object rusi <rustompmody@gmail.com> - 2013-12-12 20:53 -0800
    Re: Need help with file object John Gordon <gordon@panix.com> - 2013-12-13 05:37 +0000

#61789 — Need help with file object

FromUnix SA <d.joshi84@gmail.com>
Date2013-12-13 09:59 +0530
SubjectNeed help with file object
Message-ID<mailman.4047.1386908972.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hello,

I am facing some issue when copying or moving file

f=open('/tmp/file1')
s=open('/tmp/file2')

for line in f:
  if 'match' not in line:
     s.write(line)

import shutil
shutil.move(s, f)

With above prog I am getting error
TypeError: coercing to Unicode: need sting or buffer, file found

What that means and how I can resolve it.

Regards,
Dj

[toc] | [next] | [standalone]


#61790

Fromrusi <rustompmody@gmail.com>
Date2013-12-12 20:53 -0800
Message-ID<7f8a1809-fd66-4bd9-a005-305d483ecd63@googlegroups.com>
In reply to#61789
On Friday, December 13, 2013 9:59:25 AM UTC+5:30, Unix SA wrote:
> s=open('/tmp/file2')

<snipped>

>    s.write(line)

Among other things you are missing a write mode 

(2nd optional argument to open)

http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

[toc] | [prev] | [next] | [standalone]


#61793

FromJohn Gordon <gordon@panix.com>
Date2013-12-13 05:37 +0000
Message-ID<l8e6ds$7kb$1@reader1.panix.com>
In reply to#61789
In <mailman.4047.1386908972.18130.python-list@python.org> Unix SA <d.joshi84@gmail.com> writes:

> f=open('/tmp/file1')
> s=open('/tmp/file2')

> for line in f:
>   if 'match' not in line:
>      s.write(line)

> import shutil
> shutil.move(s, f)

> With above prog I am getting error
> TypeError: coercing to Unicode: need sting or buffer, file found

> What that means and how I can resolve it.

shutil.move() expects filename arguments, not open file objects.

Also, since your open() statements are missing the file mode ('r' or 'w'),
they will both open in the default read mode.

Also, it really helps if you post the real code and real error message.
Don't type them from memory.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web