[vox-tech] python: slicing extensions from file names
Henry House
vox-tech@lists.lugod.org
Tue, 29 Jul 2003 21:48:03 -0700
--SLDf9lqlvOQaIe6s
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
On Tue, Jul 29, 2003 at 10:46:23AM -0700, David Siedband wrote:
> I'd like to find a way to slice the extension off of a file name in pytho=
n.
>=20
> That is, I'd like to slice everything after the first period in the file=
=20
> name.
Most people use regular expression to do this sort of job. I don't know
python, but here is a perl example:
foreach $filename (@array_of_filenames) {
$filename ~=3D s/\..+$//;
# $filename now contains new name. insert code to use new name here
}
Perhaps someone who knows python would be kind enough to provide a python
example.
By the way, renaming files by removing a specific extension is easy in the
shell. To rename bunch of files named <name>.HTM to <name>.html, try
for file in *.HTM; do
mv $file `basename $file .HTM`.html
done
But removing any extension regardless of its content requires fuzzy matchin=
g,
such as a regular expression.
--=20
Henry House
The unintelligible text that may follow is a digital signature.=20
See <http://hajhouse.org/pgp> for information. My OpenPGP key:
<http://hajhouse.org/hajhouse.asc>.
--SLDf9lqlvOQaIe6s
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQE/J04Di3lu92AVGcIRAnqxAJwJTEE6zuUgUksaq1eyEeUlWdKAjgCcCpOq
gcUdS7stebvSVkKpI+M9dj8=
=b2SW
-----END PGP SIGNATURE-----
--SLDf9lqlvOQaIe6s--