英文:
How to get extract directory name from the full filepath name of a file using glob recursive
问题
1,./images/trainnew/dogs/dogs2.jpg,dogs
2,./images/trainnew/dogs/dogs1.jpg,dogs
3,./images/trainnew/cats/cats2.jpg,cats
4,./images/trainnew/cats/cats1.jpg,cats
英文:
I have two folders cats and dogs with 5 jpeg images in each
cat1.jpeg cat2.jpeg..... dog1.jpeg dog2.jpeg etc...
I want to write a CSV file that looks like this
1,./images/trainnew/dogs/dogs2.jpg,dogs
2,./images/trainnew/dogs/dogs1.jpg,dogs
3,./images/trainnew/cats/cats2.jpg,cats
4,./images/trainnew/cats/cats1.jpg,cats
Where entry dogs and cats at then end of each line refers to the name of the folders dogs and cats within the folder trainnew/
using the code below
from glob import glob
import os
count =1
f = open('test7.csv', 'w')
for filename in glob('./images/trainnew/**/*.jpg', recursive=True):
f.write(str(count) + ',' + filename + ',\n')
print(filename)
count +=1
===================
My test7.csv looks like this
1,./images/trainnew/dogs/dogs2.jpg,
2,./images/trainnew/dogs/dogs1.jpg,
3,./images/trainnew/cats/cats2.jpg,
4,./images/trainnew/cats/cats1.jpg,
How do i get the respective directory names and print at the end of each of these lines respectively in the csv file.
答案1
得分: 0
from glob import glob
import os
count = 1
with open('test7.csv', 'w') as f:
for filename in glob('./images/trainnew/**/*.jpg', recursive=True):
dirname = os.path.dirname(filename) # 获取当前文件的目录名
foldername = os.path.basename(dirname) # 获取该目录路径的基本名称
f.write(str(count) + ',' + filename + ',' + foldername + '\n')
print(filename)
count += 1
os.path.dirname(filename)
行获取当前文件的目录(例如'./images/trainnew/dogs'或'./images/trainnew/cats'),os.path.basename(dirname)
返回该路径的基本名称(例如'dogs'或'cats')。
英文:
from glob import glob
import os
count = 1
with open('test7.csv', 'w') as f:
for filename in glob('./images/trainnew/**/*.jpg', recursive=True):
dirname = os.path.dirname(filename) # Get the directory name of the file
foldername = os.path.basename(dirname) # Get the base name from that directory path
f.write(str(count) + ',' + filename + ',' + foldername + '\n')
print(filename)
count +=1
The os.path.dirname(filename)
line retrieves the directory of the current file (like './images/trainnew/dogs' or './images/trainnew/cats'), and os.path.basename(dirname)
give u the base name of that path (like 'dogs' or 'cats').
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论