Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

URL List Sort By Filename

DZone's Guide to

URL List Sort By Filename

·
Free Resource
This little script sorts a url list by the filename of an url.


#! /usr/bin/python
import sys
filename = sys.argv[1]

f=open(filename, 'r')
contents=f.read()
url_list = contents.split('\n')

def get_filename(file):
    return file[file.rfind('/')+1:]

def filename_compare(x, y):
    file_x = get_filename(x)
    file_y = get_filename(y)
    if file_x > file_y:
        return 1
    elif file_x == file_y:
        return 0
    else:
        return -1

url_list.sort(filename_compare)

for url in url_list:
    if url != '':
        print url
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}