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


Groups > comp.lang.python > #73386

Re: Python Fails to Write to File

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <paul@mcnettware.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.025
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'subject:Python': 0.06; 'context': 0.07; 'error:': 0.07; 'referring': 0.07; 'tkinter': 0.07; 'api': 0.11; 'def': 0.12; "'w')": 0.16; 'subject:File': 0.16; 'tkinter.': 0.16; 'typeerror:': 0.16; 'exception': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'file,': 0.19; 'skip:f 30': 0.19; 'written': 0.21; 'help.': 0.21; 'header:User- Agent:1': 0.23; 'paul': 0.24; 'skip:" 20': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'file:': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; 'text': 0.33; 'open': 0.33; 'skip:d 20': 0.34; 'created': 0.35; 'but': 0.35; 'google': 0.35; 'in:': 0.36; 'should': 0.36; 'being': 0.38; 'manager': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'realize': 0.39; 'received:71': 0.39; 'though,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'then,': 0.60; 'received:208': 0.61; 'you.': 0.62; 'name': 0.63; 'such': 0.63; 'more': 0.64; 'love': 0.65; 'experience.': 0.67; 'close': 0.67; 'invalid': 0.68; 'caused': 0.69; 'to,': 0.72; 'as:': 0.81; 'received:208.70': 0.91; 'step.': 0.91
Date Wed, 18 Jun 2014 16:20:23 -0700
From Paul McNett <paul@mcnettware.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Python Fails to Write to File
References <8aaa4195-af5c-4ba3-a891-b1f268c086ac@googlegroups.com>
In-Reply-To <8aaa4195-af5c-4ba3-a891-b1f268c086ac@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11126.1403133632.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1403133632 news.xs4all.nl 2909 [2001:888:2000:d::a6]:40715
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73386

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web