Guest Post: A Response to Directory to Webpage

3 November 2007

In the last post, I talked about how I turned a directory of PDFs to an HTML-list. An example of the filenames was C_L_Warrior.pdf, which I turned into a simple index of files by author.

dbr, a warrior who frequently comments here, has written in with some improvements. Over to him:

Hello. I'm not 100% sure what the section:

i.split('.')[0].replace('_', ' '):

is supposed to do, but I assume it's supposed to split the extension off the file, and replace all underscores with spaces - Assuming I'm right, it doesn't work with multi-extension files, like "QS.3814.dmg.part" (a file I happened to have in the same folder as I quickly ran the code..)

'.'.join(i.split('.')[:-1]).replace('_',' ')

seems a little convoluted, but basically it splits the filename, and rejoins them excluding the final extension.

And since shoving everything in one line is fun, the first version of the code as a list-comprehension (which also happens to be faster):

print '\n'.join(['<li><a href ="%s">%s</a></li>' %
(i, '.'.join(i.split('.')[:-1]).replace('_',' '))

for i in os.listdir('./')])

Let's measure the speed of the code, the first is the original code, the second is as a list-comprehension.

[dbr@schism:~/Desktop]$ time python listdir.py
[...]
real 0m0.222s
[dbr@schism:~/Desktop]$ time python listdir_listcomp.py
[...]
real 0m0.079s

And since I'm extremely bored, as a bash-script:

for cfile in `ls`; do echo "<li><a href=""$cfile"">
$(echo $cfile|sed -e 's/\.[^.]*$//')</a></li>"; done

1 Felix says...

The Bash script can be a little shorter:

for cfile in *;
  do
  echo "<li><a href=\"$cfile\">${cfile%%.*}</a></li>";
done

Posted at 3:28 a.m. on November 4, 2007


What do you have to say?

Show Editing Help


About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Nui

July 18, 2008
Hmm, this would be more persuasive as an argument with some evidence. I am a happy admin of Windows and a novice user of Linux, so I have taken the ...
Give Linux a chance

Paddy3118

July 18, 2008
Hi, I too work with Electronic Design Automation tools, where Tcl is used extensively. I tend to only occasionally have to write in Tcl and so find the TclTutor utility: ...
Python and TCL

Cliff Wells

July 17, 2008
I personally cannot live without the Web Developer extension or Firebug. Unfortunately these are probably both among the more difficult to port extensions. Given how poorly Firefox functions on Linux ...
Will Epiphany be able to compete with Firefox's extensions?

making money on the internet

July 17, 2008
[url=http://www.divinecaroline.com/public/user/profile?user_id=83997]extra money 101waystoincome.com[/url]
A year after my 2007 predictions - the score card

Leatherjackets99

July 16, 2008
New Style in Leather Jackets For Man and Woman at http://www.Leatherjackets99.com They Offer Free Shipment Worldwide.
Email Syntax Check in Python

Åke Forslund

July 13, 2008
I'm pretty much a novice in both of these languages but I find them both easy to use and preform the tasks I give them. However I rarely use them ...
Python and TCL

Christopher Thoday

July 12, 2008
A single test is not sufficient to give you confidence that the algorithm is working. You should make 'number' an argument of 'main' so that you can test some boundary ...
Python and TCL

paul21

July 10, 2008
Shame on Mozilla. They should make developers specify the extension license before hosting it. They should show the license next to download button as well.
Are your Firefox extensions proprietary software?

Tris

July 8, 2008
Justin - You say they had not heard of Linux? That doesn't sound very professional to me!
Give Linux a chance

michael

July 8, 2008
what about Galeon? in Gnome i use Galeon mostly. it is fast and stable and has a nice portal with search masks for Debian, FSF, Freshmeat and so on. wtf ...
Will Epiphany be able to compete with Firefox's extensions?

vermin

July 7, 2008
> Eventually, after a bit of digging and Googling, I found their Toolbar-License... You simply found the license of the StumbleUpon Toolbar for Internet Explorer. This is another product, much ...
Are your Firefox extensions proprietary software?

Andrew West

July 6, 2008
Both the Python and the Tcl example could do with error checking. While at first this may not seem on topic with the post I think it better shows the ...
Python and TCL

Kurushiyama

June 30, 2008
XML is no replacement for SGML, it's a subset.
An Introduction to ReStructuredText