[Tutor] deleting one line in multiple files

bhaaluu bhaaluu at gmail.com
Fri Sep 14 03:58:43 CEST 2007


Greetings,
I'm running Python 2.4.3 on a GNU/Linux box.

This question is about using 'fileinput.'

I have a directory of files, and I've created a file list
of the files I want to work on:

$ ls > file.list

Each file in file.list needs to have a line removed,
leaving the rest of the file intact.

I found this snippet on the Net, and it works fine for one file:

# the lines with '<script type' are deleted.
import fileinput

for line in fileinput.input("file0001.html", inplace=1):
    line = line.strip()
    if not '<script type'in line:
        print line

The docs say:
This iterates over the lines of all files listed in sys.argv[1:]...
I'm not sure how to implement the argv stuff.

However, the documentation also states:
To specify an alternative list of filenames,
pass it as the first argument to input().
A single file name is also allowed.

So, when I replace file0001.html with file.list (the alternative list
of filenames, nothing happens.

# the lines with '<script type' are deleted.
import fileinput

for line in fileinput.input("file.list", inplace=1):
    line = line.strip()
    if not '<script type'in line:
        print line

file.list has one filename on each line, ending with a newline.
file0001.html
file0002.html
:::
:::
file0175.html

Have I interpreted the documentation wrong?
The goal is to delete the line that has '<script type' in it.
I can supply more information if needed.
TIA.
-- 
bhaaluu at gmail dot com


More information about the Tutor mailing list