Getting file names without file extensions with glob

python get filename with extension
python get filename from directory
python get filename from path
python get current file name
python get file type without extension
python get file name and directory
python rename file without extension
python glob

I'm searching for .txt files only

from glob import glob
result = glob('*.txt')

>> result
['text1.txt','text2.txt','text3.txt']

but I'd like result without the file extensions

>> result
['text1','text2','text3']

Is there a regex pattern that I can use with glob to exclude the file extensions from the output, or do I have to use a list comprehension on result?

There is no way to do that with glob(), You need to take the list given and then create a new one to store the values without the extension:

import os
from glob import glob

[os.path.splitext(val)[0] for val in glob('*.txt')]

os.path.splitext(val) splits the file names into file names and extensions. The [0] just returns the filenames.

Python, How do I get filenames without an extension in Python? There is no way to do that with glob(), You need to take the list given and then create a new one to store the values without the extension: import os from glob import glob [os.path.splitext(val) for val in glob('*.txt')] os.path.splitext(val) splits the file names into file names and extensions. The just returns the filenames.

Since you’re trying to split off a filename extension, not split an arbitrary string, it makes more sense to use os.path.splitext (or the pathlib module). While it’s true that the it makes no practical difference on the only platforms that currently matter (Windows and *nix), it’s still conceptually clearer what you’re doing. (And if you later start using path-like objects instead of strings, it will continue to work unchanged, to boot.)

So:

paths = [os.path.splitext(path)[0] for path in paths]

Meanwhile, if this really offends you for some reason, what glob does under the covers is just calling fnmatch to turn your glob expression into a regular expression and then applying that to all of the filenames. So, you can replace it by just replacing the regex yourself and using capture groups:

rtxt = re.compile(r'(.*?)\.txt')
files = (rtxt.match(file) for file in os.listdir(dirpath))
files = [match.group(1) for match in files if match]

This way, you’re not doing a listcomp on top of the one that’s already in glob; you’re doing one instead of the one that’s already in glob. I’m not sure if that’s a useful win or not, but since you seem to be interested in eliminating a listcomp…

Working With Files in Python – Real Python, How do I get a list of files in a directory in Python? If you need a list of filenames that all have a certain extension, prefix, or any common string in the middle, use glob instead of writing code to scan the directory contents yourself. The pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules.

Use index slicing:

result = [i[:-4] for i in result]

Get Filename Without Extension in Python, In most cases, you shouldn't use a regex for that. os.path.splitext(filename)[0]. This will also handle a filename like .bashrc correctly by keeping  Python Glob() Function To Match Path, Directory, File Names with Examples 26/06/2018 26/06/2018 by İsmail Baydan glob is a general term used to define techniques to match specified pattern according to rules related Unix shell.

Another way using rsplit:

>>> result = ['text1.txt','text2.txt.txt','text3.txt']
>>> [x.rsplit('.txt', 1)[0] for x in result]
['text1', 'text2.txt', 'text3']

You could do as a list-comprehension:

result = [x.rsplit(".txt", 1)[0] for x in glob('*.txt')]

Python Glob without the whole path - only the filename, If you need a list of filenames that all have a certain extension, prefix, or any every pathname (file or directory) in the directory dir, without recursing further into​  Second, glob only returns the names of files that actually exist; in csh no check for existence is made unless a pattern contains a ?, *, or [] construct. When the glob command returns relative paths whose filenames start with a tilde “~” (for example through glob * or glob -tails, the returned list will not quote the tilde with “./”.

Use str.split

>>> result = [r.split('.')[0] for r in glob('*.txt')]
>>> result
['text1', 'text2', 'text3']

glob – Filename pattern matching, This function will return 2 parts: the filename part and the extension part. Here is how to use it. #!/usr/bin/python3 import os # File path example. PHP, get file name without file extension. I have this PHP code: If I have a file named my.zip, this function returns .zip. I want to do the reverse, I want the function to return my without the extension.

10.7. glob — Unix style pathname pattern expansion, Note that unlike fnmatch.fnmatch() , glob treats filenames beginning with a dot ( . ) Return an iterator which yields the same values as glob() without actually a directory containing only the following files: 1.gif , 2.txt , and card.gif . glob() will  I have used Input::file('upfile')->getClientOriginalName() to retrieve name of uploaded file but gives name with extension like qwe.jpg.How do I get name without extension like qwe in laravel. Stack Overflow

Information about Files - file, glob, glob provides the access to the names of files in a directory. It uses a name matching Returns file name without extension; split .. Split the string into  Get the directory name, filename and extension with pathinfo() PHP’s pathinfo() function returns an associative array containing the basename, dirname, extension and (from PHP 5.2.0) the filename without the extension.

Python : How to remove files by matching pattern | wildcards, Remove files by pattern using glob.glob() & os.remove() Get a list of all the file paths that ends with .txt from in specified directory therefore it will find files of matching pattern in given directory only not the sub directories. C++: How to get filename from a path with or without extension | Boost | C++17  Returns the file name of the specified path string without the extension. GetFileNameWithoutExtension(ReadOnlySpan<Char>) Returns the file name without the extension of a file path that is represented by a read-only character span.

Comments
  • Your title is a little misleading, would Getting file names without file extension with glob be better?
  • @Simon Agreed. The title as is sounds like it’s looking for libmagic or some other heuristic way to detect text files. And your rewrite sounds good to me.
  • @abarnert Should I edit it myself? Without OP consent?
  • As a side note: glob doesn’t take regex patterns, it takes glob patterns, which are a different thing. Under the covers, it does build regex patterns to apply to a list of files, but you don’t get to see those. (If you want to see them, see the fnmatch module.)
  • @Simon I think that’s fine. The OP can always revert if they disagree, and if not, you’ve improved their question for them.
  • @abanert: Thanks for the detailed explanation.
  • This works, but it’s not as clear or robust as using splitext, or even rsplit. If you later change the code to accept both .txt and .text, for example, the other solutions keep working, but this one doesn’t.
  • Why not just rsplit('.')?
  • @abarnert sure that's working too. But I think its better use .txt as OP is specific about .txt files.
  • This will do the wrong thing for, say, text.file.10.txt, returning text instead of text.file.10 (which would be correct on some obsolete platforms, but not on Windows or Unix).