Python - Glob without absolute path

I am using glob to get the image pair names for a lot of images. The only problem is I am getting the absolute path and I don't want that, I only want the image names. How can i do this?

import glob

A=sorted(glob.glob('/media/test/A*.png'))
B=sorted(glob.glob('/media/test/B*.png'))
NumbSeq=len(A)
for i in range(0,NumbSeq):
  print "\"%s\",\"%s\","%(A[i],B[i])

      

I get:

 "/media/test/A170900_85495.460376.png","/media/test/B170900_85495.460376.png"

      

I need to:

 "A170900_85495.460376.png","B170900_85495.460376.png"

      

+3


source to share


3 answers


try it

os.path.basename(A[i])

      

this will only return the filename.



import glob
import os

A=sorted(glob.glob('/media/test/A*.png'))
B=sorted(glob.glob('/media/test/B*.png'))
NumbSeq=len(A)

for i in range(0,NumbSeq):
  print '"%s","%s"'%(os.path.basename(A[i]),os.path.basename(B[i]))

      

This will give you

"A170900_85495.460376.png","B170900_85495.460376.png"

      

+1


source


Pass each name in A

and B

before os.path.basename

before printing them:



for i in range(0,NumbSeq):
    print '"%s","%s"'%(os.path.basename(A[i]),os.path.basename(B[i]))

      

+1


source


Instead, glob(pathname)

use a method glob1(dirname, pattern)

to get the filenames.

>>> glob.glob1("some_dir", "*.png")
['foo.png', 'bar.png', ...]

>>> glob.glob("some_dir/*.png")
['/home/michael/A_dir/B_dir/some_dir/foo.png',
'/home/michael/A_dir/B_dir/some_dir/bar.png',
...]

      

0


source







All Articles