Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73663
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subsequent': 0.05; 'sufficient': 0.05; 'subject:Python': 0.06; 'sys': 0.07; 'cursor': 0.09; 'file)': 0.09; 'positioned': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'second.': 0.09; 'def': 0.12; "'''": 0.16; "'w')": 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'script,': 0.16; 'string:': 0.16; 'subject:writing': 0.16; 'written.': 0.16; '(you': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'example': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'pointer': 0.24; 'file.': 0.24; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; 'code': 0.31; 'prints': 0.31; 'file': 0.32; 'beginning': 0.33; 'there': 0.35; 'version': 0.36; 'work?': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'new': 0.61; 'course': 0.61; 'simple': 0.61; 'first': 0.61; 'back': 0.62; 'complete': 0.62; 'reached': 0.63; 'skip:w 30': 0.69; 'aws': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Python reading and writing |
| Date | Fri, 27 Jun 2014 18:25:55 +0200 |
| Organization | None |
| References | <6dcdb0ea-387b-4b32-92a8-a4197db5cff9@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd94b3.dip0.t-ipconnect.de |
| User-Agent | KNode/4.11.5 |
| 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.11290.1403886377.18130.python-list@python.org> (permalink) |
| Lines | 54 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1403886377 news.xs4all.nl 2855 [2001:888:2000:d::a6]:49095 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:73663 |
Show key headers only | View raw
aws Al-Aisafa wrote:
> Why doesn't this code work?
> #I want this code to write to the first file and then take the
> #contents of the first file and copy them to the second.
>
> from sys import argv
>
> script, file1, file2 = argv
>
>
> def write_to_file(fileread, filewrite):
> '''Writes to a file '''
> filewrite.write(fileread)
>
>
> input_file = open(file1, 'r+w')
> output_file = open(file2, 'w')
>
> datain = raw_input(">")
> input_file.write(datain)
>
> print input_file.read()
>
> write_to_file(input_file.read(), output_file)
> create a new version of this paste
> http://pastebin.com/A3Sf9WPu
> input_file.write(datain)
The file cursor is now positioned after the data you have just written. Then
you read all data in the file *after* that data. As there is not data (you
have reached the end of the file) the following prints the empty string:
> print input_file.read()
One way to fix this is to move the file pointer back to the beginning of the
file with
input_file.seek(0)
so that a subsequent
input_file.read()
will return the complete contents of the file.
For the simple example it would of course be sufficient to reuse datain:
write_to_file(datain, output_file)
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Python reading and writing aws Al-Aisafa <geniusrko@gmail.com> - 2014-06-27 09:12 -0700 Re: Python reading and writing Peter Otten <__peter__@web.de> - 2014-06-27 18:25 +0200
csiph-web