Python glob, os, relative path, making filenames into a list

Viewed 21102

I am trying to make a list of all files in a directory with filenames in a that end in .root.

After reading some writings in the forum I tried to basic strategies using glob and os.listdir but I got into trouble for both of them

First, when I use

import glob
filelist = glob.glob('/home/usr/dir/*.root')

It does make a list of string with all filenames that end in .root but I still face a problem.

I would like to be the list of string to have filenames as '/dir/.root' but the string has full path '/home/usr/dir/.root'

Second, if I use os.listdir, I get into the trouble that

  path = '/home/usr/'
  filelist = os.listdir(path + 'dir/*.root')
  syntax error

which tells me that I can not only get the list of files for .root.

In summary, I would like to make a list of filenames, that end in .root and are in my /home/usr/dir, while cutting off the '/home/usr' part. If I use globe, I get into the trouble of having /home/usr/. If I use os.listdir, I can't specify ".root" endling.

2 Answers
Related