File processing

Ken Seehof kens at sightreader.com
Tue Jul 10 05:07:39 EDT 2001


import os

path = '/junk'   # put your directory here

for fname in os.listdir(path):  # list of files in your directory
    f = open(fname, 'r')
    lines = f.readlines()       # reads a list of lines
    f.close()
    f = open(fname, 'w')
    f.writelines(lines[1:])     # writes all but the first line
    f.close()


Now, be careful!  This will remove the first line of every file
in the directory, including files you don't want to ruin.  Back
up everything first!

----- Original Message -----
From: "Chris McMillan" <christopherjmcmillan at eaton.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Monday, July 09, 2001 2:01 PM
Subject: File processing


> Hello all!
>
> I'm trying to write a script that will open a file, delete the first line,
> and then save the file with the same name.  Can someone please point me in
> the right direction?  Thanks!
>
> Why am I doing this you may ask: I have tens of data files that I need to
> strip the first line before I can process it in Matlab.  So, I dream of a
> script that opens every file in a directory, deletes the first line, .....
>
> Thanks,
>
> Chris
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list