1

i have a list with filenames. How do i use the os.path.splitext() function to split the suffix and a filename and wirte it again into a list without the suffix.

import os
image_names = os.listdir('C:\Pictures\Sample Pictures')
newimageList = []
for name in image_names:
  newimageList.append(name.os.path.splitext())
print(newList)

unfortunately the code don't work i get an Error: 'str' object has no attribute 'os'

if i had a new list(separeted in filename and suffix) i would skip every second element in a list to get just the filenames

Gregor Ostrov
  • 45
  • 1
  • 5

2 Answers2

1

According to the docs, you should try os.path.splitext('/desired_path').

You just have to adjust your code...

So, instead of:

newimageList.append(name.os.path.splitext())

Use:

newimageList.append(os.path.splitext(name))
dot.Py
  • 5,007
  • 5
  • 31
  • 52
  • Correct, but it is not 'according to the docs', it is more of a python grammar issue: It is not `x.a.b()` but instead `a.b(x)` – UltraInstinct Jul 19 '16 at 20:35
1

You want os.path.splitext(name) instead of name.os.path.splitext().

Hardik
  • 21
  • 2