On Friday 2011 July 15 15:58, Richard D. Moores wrote:
> On Fri, Jul 15, 2011 at 14:47, Stefan Behnel <stefan...@behnel.de> wrote:
> > Richard D. Moores, 15.07.2011 23:21:
> >> What do I do to test.txt to make it "an object with a write(string)
> >> method"?
> >
> > Oh, there are countless ways to do that, e.g.
> >
> >  class Writable(object):
> >      def __init__(self, something):
> >          print("Found a %s" % something))
> >      def write(self, s):
> >          print(s)
> >
> >  print("Hello, world!", file=Writable("C:\\test\\test.txt"))
> >
> > However, I'm fairly sure what you want is this:
> >
> >    with open("C:\\test\\test.txt", "w") as file_object:
> >        print("Hello, world!", file=file_object)
>
> Yes, went with
>
> with open("C:\\test\\test.txt", "a+") as file_object:
>       print("Hello, world!", file=file_object)
>
> > Look up "open()" (open a file) and the "with statement" (used here
> > basically as a safe way to make sure the file is closed after writing).
> >
> > Also note that "\t" refers to a TAB character in Python, you used this
> > twice in your file path string.

I believe on Windows, you can almost always use a forward slash in a path: 
C:/somewhere/somewhereelse/

-- 
I have seen the future and I am not in it.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to