Strange behaviour with a for loop.

Sean Murphy mhysnq1964 at icloud.com
Fri Jan 3 23:03:35 EST 2014


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 




More information about the Python-list mailing list