File processing
Ken Seehof
kens at sightreader.com
Tue Jul 10 05:39:22 EDT 2001
Oops you need this:
import os
path = r'D:\Dev\junk' # put your directory here
for fname in os.listdir(path): # list of files in your directory
fname = os.path.join(path, fname) # add the directory to the file name
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()
----- Original Message -----
From: "Ken Seehof" <kens at sightreader.com>
To: <python-list at python.org>; "Chris McMillan"
<christopherjmcmillan at eaton.com>
Sent: Tuesday, July 10, 2001 2:07 AM
Subject: Re: File processing
> 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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list