0

This posting shows how to add all files in a directory to an list in python

How to list all files of a directory?

onlyfiles = [ f for f in listdir(src_dir) if isfile(join(src_dir,f)) ]

How can I remove the file extension in the onlyfiles list?

Community
  • 1
  • 1
n179911
  • 18,457
  • 41
  • 111
  • 158

1 Answers1

3

You can use os.path.splitext to split a filename string into the filename and the extension.

from os.path import join, splitext    

onlyfiles = [splitext(f)[0] for f in listdir(src_dir) if isfile(join(src_dir,f)) ]

Note that splitext will effectively split at the "final" extension. So if you had a filename "filename.pdf.txt" it will split it into ("filename.pdf", "txt").

Ffisegydd
  • 47,839
  • 14
  • 137
  • 116