Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73384 > unrolled thread
| Started by | cutey Love <cuteywithlove@gmail.com> |
|---|---|
| First post | 2014-06-18 16:03 -0700 |
| Last post | 2014-06-19 09:39 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
Python Fails to Write to File cutey Love <cuteywithlove@gmail.com> - 2014-06-18 16:03 -0700
Re: Python Fails to Write to File Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-18 17:18 -0600
Re: Python Fails to Write to File Paul McNett <paul@mcnettware.com> - 2014-06-18 16:20 -0700
Re: Python Fails to Write to File cutey Love <cuteywithlove@gmail.com> - 2014-06-19 00:54 -0700
Re: Python Fails to Write to File Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-06-19 09:39 +0100
| From | cutey Love <cuteywithlove@gmail.com> |
|---|---|
| Date | 2014-06-18 16:03 -0700 |
| Subject | Python Fails to Write to File |
| Message-ID | <8aaa4195-af5c-4ba3-a891-b1f268c086ac@googlegroups.com> |
I'm trying to write data to a text file
But I'm getting the error:
TypeError: invalid file: <_io.TextIOWrapper
Code is
def saveFile():
file_path = filedialog.asksaveasfile(mode='w', filetypes=[('text files', '.txt')], defaultextension=".txt")
fo = open(file_path, 'w')
for e in myList:
fo.write(e)
fo.close()
The file is being created if not already present but no data is written
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-06-18 17:18 -0600 |
| Message-ID | <mailman.11125.1403133566.18130.python-list@python.org> |
| In reply to | #73384 |
On Wed, Jun 18, 2014 at 5:03 PM, cutey Love <cuteywithlove@gmail.com> wrote: > I'm trying to write data to a text file > > But I'm getting the error: > > TypeError: invalid file: <_io.TextIOWrapper Post the full traceback. By posting only the error message you're removing useful information.
[toc] | [prev] | [next] | [standalone]
| From | Paul McNett <paul@mcnettware.com> |
|---|---|
| Date | 2014-06-18 16:20 -0700 |
| Message-ID | <mailman.11126.1403133632.18130.python-list@python.org> |
| In reply to | #73384 |
On 6/18/14, 4:03 PM, cutey Love wrote:
> I'm trying to write data to a text file
>
> But I'm getting the error:
>
> TypeError: invalid file: <_io.TextIOWrapper
Always better to err on posting too much context (the entire traceback)
than too little.
> Code is
>
> def saveFile():
> file_path = filedialog.asksaveasfile(mode='w', filetypes=[('text files', '.txt')], defaultextension=".txt")
> fo = open(file_path, 'w')
I used Google (searched on "filedialog.asksaveasfile") to realize that
you are likely using Tkinter. I then used Google to find out the API for
filedialog.asksaveasfile() and I found that it returns the ALREADY OPEN
FILE for you to write to, not the name of the file you are writing to.
Referring to that same documentation, I see there's another function you
are probably more interested in: filedialog.asksaveasfilename(). Then,
you can open the file, write to it, and close it and be in control of
every step. Not sure which is preferable, though, as I have no (recent)
Tkinter experience.
>
> for e in myList:
> fo.write(e)
>
>
> fo.close()
You should use a context manager to open, write to, and close files.
Such as:
with open(file_path, 'w') as fo:
for e in myList:
fo.write(e)
> The file is being created if not already present but no data is written
filedialog.asksaveasfile() was opening it for you. It was being closed
automatically when file_path fell out of scope (after the exception
caused by your fo = open(file_path, 'w') line.
My response was intended to help.
Paul
[toc] | [prev] | [next] | [standalone]
| From | cutey Love <cuteywithlove@gmail.com> |
|---|---|
| Date | 2014-06-19 00:54 -0700 |
| Message-ID | <627b76ee-b5e1-4969-b8e3-ff5b9bbcf97b@googlegroups.com> |
| In reply to | #73384 |
Thank you very much, that fixed it.
On Thursday, June 19, 2014 12:03:43 AM UTC+1, cutey Love wrote:
> I'm trying to write data to a text file
>
>
>
> But I'm getting the error:
>
>
>
> TypeError: invalid file: <_io.TextIOWrapper
>
>
>
> Code is
>
>
>
> def saveFile():
>
> file_path = filedialog.asksaveasfile(mode='w', filetypes=[('text files', '.txt')], defaultextension=".txt")
>
> fo = open(file_path, 'w')
>
>
>
> for e in myList:
>
> fo.write(e)
>
>
>
>
>
> fo.close()
>
>
>
> The file is being created if not already present but no data is written
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-06-19 09:39 +0100 |
| Message-ID | <mailman.11136.1403167154.18130.python-list@python.org> |
| In reply to | #73403 |
On 19/06/2014 08:54, cutey Love wrote: > Thank you very much, that fixed it. > What do you not understand about top posting and using google groups? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web