Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63107 > unrolled thread
| Started by | Sean Murphy <mhysnq1964@icloud.com> |
|---|---|
| First post | 2014-01-04 15:03 +1100 |
| Last post | 2014-01-04 13:25 -0800 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Strange behaviour with a for loop. Sean Murphy <mhysnq1964@icloud.com> - 2014-01-04 15:03 +1100
Re: Strange behaviour with a for loop. Larry Hudson <orgnut@yahoo.com> - 2014-01-04 13:25 -0800
| From | Sean Murphy <mhysnq1964@icloud.com> |
|---|---|
| Date | 2014-01-04 15:03 +1100 |
| Subject | Strange behaviour with a for loop. |
| Message-ID | <mailman.4887.1388811827.18130.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2014-01-04 13:25 -0800 |
| Message-ID | <ALqdnQgXzdDc4VXPnZ2dnUVZ_u6dnZ2d@giganews.com> |
| In reply to | #63107 |
On 01/03/2014 08:03 PM, 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
>
How easy it is to overlook your own typos... (No worry, everybody does it) ;-)
In your first version you have: filenames = sys.argv[1:]
which gives you a list of filenames (which is what you want).
In your second version you have: filenames = sys.argv[1]
which is ONE item -- a string, not a list. You left out the colon.
-=- Larry -=-
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web