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


Groups > comp.lang.python > #63111

Re: Strange behaviour with a for loop.

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python.': 0.02; 'from:addr:yahoo.co.uk': 0.04; 'parameters': 0.04; 'element': 0.07; 'modify': 0.07; 'sys': 0.07; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'lawrence': 0.09; 'newly': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spelled': 0.09; 'wrong,': 0.09; 'language.': 0.14; 'question.': 0.14; '%s\\n"': 0.16; 'filename)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'referencing': 0.16; 'unlikely': 0.16; 'index': 0.16; 'all.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'result.': 0.19; 'slightly': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'sean': 0.24; 'script': 0.25; 'shown': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'array': 0.29; 'character': 0.29; 'code': 0.31; 'getting': 0.31; 'os,': 0.31; 'languages': 0.32; "i'd": 0.34; 'subject:with': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'results.': 0.60; 'providing': 0.61; "you've": 0.63; 'our': 0.64; 'different': 0.65; 'results': 0.69; 'wish': 0.70
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Strange behaviour with a for loop.
Date Sat, 04 Jan 2014 05:38:20 +0000
References <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-147-28-114.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com>
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.4891.1388813882.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388813882 news.xs4all.nl 2873 [2001:888:2000:d::a6]:35008
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63111

Show key headers only | View raw


On 04/01/2014 04:03, Sean Murphy wrote:
> Hello all.
>
> This is a newly question. But I wish to understand why the below code is providing different results.
>
> import os, sys
>
>
> if len(sys.argv) > 2:
>    filenames = sys.argv[1:]
> else
>    print ("no parameters provided\n")
>    sys.edit()
>
> for filename in filenames:
>    print ("filename is: %s\n" %filename)
>
> The above code will return results like:
>
> filename is test.txt
>
> If I modify the above script slightly as shown below, I get a completely different result.
>
> if len(sys.argv) > 2:
>    filenames = sys.argv[1]
> else
>    print ("no parameters provided\n")
>    sys.exit()
>
> for filename in filenames:
>    print ("filename is:  %s\n" % filename)
>
> The result is the filename is spelled out a character at a time. The bit I am missing is something to do with splicing or referencing in Python.
>
> Why am I getting different results? In other languages I would have got the whole content of the element when using the index of the array (list).
>
>
> Sean
> filename is: t
> filename
>

As you've already had answers I'd like to point out that your test for 
len(sys.argv) is wrong, else is missing a colon and sys.edit() is very 
unlikely to work :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

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


Thread

Re: Strange behaviour with a for loop. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-04 05:38 +0000

csiph-web