如何使用递归的glob来从文件的完整文件路径名中提取目录名

huangapple go评论71阅读模式
英文:

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').

huangapple
  • 本文由 发表于 2023年7月23日 17:02:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定