Assume my folder structure to be
+Data
-abc.jpg
-db.jpg
-ap.jpg
Input is 'path/to/Data'
Expected output is ['abc','db','ap']
I saw many similar questions but did not get what exactly I wanted. I prefer to use os module in python.
Assume my folder structure to be
+Data
-abc.jpg
-db.jpg
-ap.jpg
Input is 'path/to/Data'
Expected output is ['abc','db','ap']
I saw many similar questions but did not get what exactly I wanted. I prefer to use os module in python.
simply try this,
l=os.listdir('path')
li=[x.split('.')[0] for x in l]
.
and take first argument.import os
files_no_ext = [".".join(f.split(".")[:-1]) for f in os.listdir() if os.path.isfile(f)]
print(files_no_ext)
import glob
from pathlib import Path
for f in glob.glob("*.*"):
print(Path(f).stem)
import os
filenames=next(os.walk(os.getcwd()))[2]
efn=[f.split('.')[0] for f in filenames]
os.getcwd() #for get current directory path
You can use os.listdir which take path as argument and return a list of files and directories in it.
>>> list_ = os.listdir("path/to/Data")
>>> list_
>>> ['abc.jpg', 'dn.jpg', 'ap.jpg']
With that list, you only have to do a comprehension list which split each element on '.' (dot), take all the elements except the last one an join them with '.' (dot) and check if the element is a file using os.path.file().
>>> list_ = ['.'.join(x.split('.')[:-1]) for x in os.listdir("path/to/Data") if os.path.isfile(os.path.join('path/to/Data', x))]
>>> list_
>>> ['abc', 'dn', 'ap']
import os
from os import listdir
from os.path import isfile, join
def filesMinusExtension(path):
# os.path.splitext(f)[0] map with filename without extension with checking if file exists.
files = [os.path.splitext(f)[0] for f in listdir(path) if isfile(join(path, f))];
return files;