Using filepath method to identify an .html page

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jan 22 08:04:41 EST 2013


On Tue, 22 Jan 2013 04:47:16 -0800, Ferrous Cranus wrote:

> htmlpage = a string respresenting the absolute path of the requested
> .html file


That is a very misleading name for a variable. The contents of the 
variable are not a html page, but a file name.

htmlpage = "/home/steve/my-web-page.html"  # Bad variable name.

filename = "/home/steve/my-web-page.html"  # Better variable name.



> What i want to do, is to associate a number to an html page's absolute
> path for to be able to use that number for my database relations instead
> of the BIG absolute path string.

Firstly, don't bother. What you consider "BIG", your database will 
consider trivially small. What is it, 100 characters long? 200? Unlikely 
to be 300, since I think many file systems don't support paths that long. 
But let's say it is 300 characters long.

That's likely to be 600 bytes, or a bit more than half a kilobyte. Your 
database won't even notice that.


> so to get an integer out of a string i would just have to type:
> 
> pin = int( htmlpage )

No, that doesn't work. int() does not convert arbitrary strings into 
numbers. What made you think that this could possibly work?

What do you expect int("my-web-page.html") to return? Should it return 23 
or 794 or 109432985462940911485 or 42?

> But would that be unique?

Wrong question.


Just tell your database to make the file name an indexed field, and it 
will handle giving every path a unique number for you. You can then 
forget all about that unique number, because it is completely irrelevant 
to you, and safely use the path while the database treats it in the 
fastest and most efficient fashion necessary.


-- 
Steven



More information about the Python-list mailing list