6

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.

Sreeragh A R
  • 2,871
  • 3
  • 27
  • 54

6 Answers6

14

simply try this,

l=os.listdir('path')
li=[x.split('.')[0] for x in l]
  1. First list your directory files
  2. split file by. and take first argument.
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
10
import os    
files_no_ext = [".".join(f.split(".")[:-1]) for f in os.listdir() if os.path.isfile(f)]
print(files_no_ext)
Keatinge
  • 4,330
  • 6
  • 25
  • 44
  • 1
    [`os.listdir`](https://docs.python.org/3.6/library/os.html#os.listdir) does not make a difference between directories and files, so your list will have (mostly empty) entries for subdirectories. – Christian König Jul 28 '17 at 07:04
  • 3
    You could furthermore exchange your `join/split` by [`os.path.splitext(f)[0]`](https://docs.python.org/3.6/library/os.path.html#os.path.splitext), which takes care of some special cases (leading periods on the basename). – Christian König Jul 28 '17 at 07:58
3
import glob
from pathlib import Path

for f in glob.glob("*.*"):
    print(Path(f).stem)
Sreeragh A R
  • 2,871
  • 3
  • 27
  • 54
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
  • 2
    Thanks for the answer, just a top-up on the answer, it is supposed `import pathlib`, then `pathlib.Path(f).stem`. – Yeo Keat Jan 17 '23 at 05:57
2
import os
filenames=next(os.walk(os.getcwd()))[2]
efn=[f.split('.')[0] for f in filenames]

os.getcwd()   #for get current directory path
Ahmad
  • 906
  • 11
  • 27
1

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']

Darkaird
  • 2,700
  • 4
  • 15
  • 29
0
    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;
grimur82
  • 149
  • 2
  • 10
  • There are some problems with you code... But, what bother me is : semi-colons ? You are in Python guy, just throw them away ! – Darkaird Jul 28 '17 at 07:15
  • Thanks for the feedback. Yes the java background seems to affect my python typing of semicolons at times. Anyway can you share what problems could arise with this code ? – grimur82 Jul 28 '17 at 07:19
  • Except semi-colons, why importing isfile and join that way without doing it for os.path.splittext ? Furthermore, you use a comprehension list for filtering (the 'if' at the right) but not for mapping. You just have to do `[os.path.splitext(f)[0] for f in listdir(path) if isfile(join(path, f))]`. – Darkaird Jul 28 '17 at 07:23
  • Okay I've done the research and you are right. My modified solution removed redundant and unnecessary code. – grimur82 Jul 28 '17 at 08:20
  • You can even not use an intermediate variable and directly return the comprehension list if you want. – Darkaird Jul 28 '17 at 08:49