Search code examples
pythonlistxrange

xrange not adhering to start, stop, step


My input file looks like:

6
*,b,*
a,*,*
*,*,c
foo,bar,baz
w,x,*,*
*,x,y,z
5
/w/x/y/z/
a/b/c
foo/
foo/bar/
foo/bar/baz/

When I use xrange, why does it not adhere to the start, stop, step method?

with open(sys.argv[1], 'r') as f:
  for _ in xrange(0, 7, 1):
    next(f)
  for listPatterns in f:
    print listPatterns.rstrip()

It outputs the text starting at line 7 when in actuality I want it to print line 1 through 7.


Solution

  • The code you want is

    with open(sys.argv[1], 'r') as f:
      for _ in xrange(0, 7, 1):
        print f.next().rstrip()
    

    The first loop you have is advancing through the file.