Getting file names without extensions
You can use Path.GetFileNameWithoutExtension
:
foreach (FileInfo fi in smFiles)
{
builder.Append(Path.GetFileNameWithoutExtension(fi.Name));
builder.Append(", ");
}
Although I am surprised there isn't a way to get this directly from the FileInfo
(or at least I can't see it).
Filename without extension terminology
It is just called a "file" if referring to its base name according to wikipedia.
https://en.wikipedia.org/wiki/Filename
How to get the filename without the extension in Java?
If you, like me, would rather use some library code where they probably have thought of all special cases, such as what happens if you pass in null or dots in the path but not in the filename, you can use the following:
import org.apache.commons.io.FilenameUtils;
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
How do I get the filename without the extension from a path in Python?
Getting the name of the file without the extension:
import os
print(os.path.splitext("/path/to/some/file.txt")[0])
Prints:
/path/to/some/file
Documentation for os.path.splitext
.
Important Note: If the filename has multiple dots, only the extension after the last one is removed. For example:
import os
print(os.path.splitext("/path/to/some/file.txt.zip.asc")[0])
Prints:
/path/to/some/file.txt.zip
See other answers below if you need to handle that case.
How to get a list of filenames(without extension) in directory in python?
import os
files_no_ext = [".".join(f.split(".")[:-1]) for f in os.listdir() if os.path.isfile(f)]
print(files_no_ext)
get filenames in a directory without extension - Python
You can use os
's splitext.
import os
path = '/Users/vivek/Desktop/buffer/xmlTest/'
files = [os.path.splitext(filename)[0] for filename in os.listdir(path)]
print (files)
Just a heads up: basename
won't work for this. basename
doesn't remove the extension.
Getting file names without file extensions with glob
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.
Using 'find' to return filenames without extension
To return only filenames without the extension, try:
find . -type f -iname "*.ipynb" -execdir sh -c 'printf "%s\n" "${0%.*}"' {} ';'
or (omitting -type f
from now on):
find "$PWD" -iname "*.ipynb" -execdir basename {} .ipynb ';'
or:
find . -iname "*.ipynb" -exec basename {} .ipynb ';'
or:
find . -iname "*.ipynb" | sed "s/.*\///; s/\.ipynb//"
however invoking basename
on each file can be inefficient, so @CharlesDuffy suggestion is:
find . -iname '*.ipynb' -exec bash -c 'printf "%s\n" "${@%.*}"' _ {} +
or:
find . -iname '*.ipynb' -execdir basename -s '.sh' {} +
Using +
means that we're passing multiple files to each bash instance, so if the whole list fits into a single command line, we call bash only once.
To print full path and filename (without extension) in the same line, try:
find . -iname "*.ipynb" -exec sh -c 'printf "%s\n" "${0%.*}"' {} ';'
or:
find "$PWD" -iname "*.ipynb" -print | grep -o "[^\.]\+"
To print full path and filename on separate lines:
find "$PWD" -iname "*.ipynb" -exec dirname "{}" ';' -exec basename "{}" .ipynb ';'
How to get only filename without extension?
If the column values are always the filename/filepath, split it from right on .
with maxsplit parameter as 1
and take the first value after splitting.
>>> df['relfilepath'].str.rsplit('.', n=1).str[0]
0 20210322636
12 factuur-f23622
14 ingram micro
19 upfront.nl domein - Copy
21 upfront.nl domein
Name: relfilepath, dtype: object
Related Topics
An Object Reference Is Required to Access Non-Static Member
SQL Command Insert Is Working But the Data Not Appear in Table
Turn a Simple C# Dll into a Com Interop Component
Benchmarking Small Code Samples in C#, Can This Implementation Be Improved
How to Install Msbuild on Os X and Linux
Filesystemwatcher with Samba on Linux
Does C++11 Have C#-Style Properties
Sharing Memory Between Two Applications
Howto Implement Callback Interface from Unmanaged Dll to .Net App
Is There a Downside to Adding an Anonymous Empty Delegate on Event Declaration
Conversion of System.Array to List
Algorithm to Compare Two Images in C#
How Does the Ternary Operator Work
Token Based Authentication in Web API Without Any User Interface