Python Forum

Full Version: .sort frustration
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I've tried so many different ways to sort but nothing is working so I've just left it as fh.sort (I know that's wrong) and I'm hoping someone can figure it out.

My code is:

1 import sys
2
3 f=open(sys.argv[1]).read().split("\n")
4 fh=open(sys.argv[2], 'wr+')
5
6 length = len(f)
7
8 for n,line in enumerate(f):
9 if "----- Begin chunk -----" in line:
10 if n == length:
11 break
12 nlist = n + 1
13 print >> fh, (nlist)
14 for m,line in enumerate(f):
15 if "----- End chunk -----" in line:
16 if m == length:
17 break
18 mlist = m + 1
19 print >> fh, (mlist)
20 fh.sort

I want to output the fh file as a numerically sorted version that looks like this:

1 1
2 5
3 50
4 75
5 100
6 150

Please and thank you!
That's the full code I've posted already.
The error I get with this code is below, but I've gotten loads of different errors trying different ways of sorting.

Traceback (most recent call last):
File "loop.py", line 21, in <module>
fh.sort()
AttributeError: 'file' object has no attribute 'sort'
Please Read BBCODE to see how to use code tags.