I would like to get the file name without its extension/suffix with split with specific character.
There are so many jpg files in directory.
for example,
A_list(B)_001.jpg
Stack_overflow_question_0.3.jpg
... and hundreds files in some directory
what I want is to get just the file NAMES without their extensions, like:
A_list(B), 001
Stack_overflow_question, 0.3
but with below code,
import os
path = 'D:\HeadFirstPython\chapter3'
os.chdir(path)
data = open('temp.txt', 'w')
for file in os.listdir(path):
if file.endswith('.jpg'):
file = file.split('_')
print(file, file=data)
data.close()
I got like below result.
['A', 'list(B)', '001.jpg']
['Stack', 'overflow', 'question', '0.3.jpg']
Can this be done with less code?
Thanks and kind regards, Tim