glob will return paths in a format matching your query, so that
glob.glob("/home/usr/dir/*.root")
# ['home/usr/dir/foo.root', 'home/usr/dir/bar.root', ...]
glob.glob("*.root")
# ['foo.root', 'bar.root', ...]
glob.glob("./*.root")
# ['./foo.root', './bar.root', ...]
...and so forth.
To get only the filename, you can use path.basename of the os module, something like this:
from glob import glob
from os import path
pattern = "/home/usr/dir/*.root"
files = [path.basename(x) for x in glob(pattern)]
# ['foo.root', 'bar.root', ...]
...or, if you want to prepend the dir part:
pattern = "/home/usr/dir/*.root"
files = [path.join('dir', path.basename(x)) for x in glob(pattern)]
# ['dir/foo.root', 'dir/bar.root', ...]
...or, if you really want the path separator at the start:
from glob import glob
import os
pattern = "/home/usr/dir/*.root"
files = [os.sep + os.path.join('dir', os.path.basename(x)) for x in glob(pattern)]
# ['/dir/foo.root', '/dir/bar.root', ...]
Using path.join and path.sep will make sure that the correct path syntax is used, depending on your OS (i.e. / or \ as a separator).
Depending on what you are really trying to do here, you might want to look at os.path.relpath, for the relative path. The title of your question indicates that relative paths might be what you are actually after:
pattern = "/home/usr/dir/*.root"
files = [os.path.relpath(x) for x in glob(pattern)]
# files will now contain the relative path to each file, from the current working directory
Answer from leo on Stack Overflowglob will return paths in a format matching your query, so that
glob.glob("/home/usr/dir/*.root")
# ['home/usr/dir/foo.root', 'home/usr/dir/bar.root', ...]
glob.glob("*.root")
# ['foo.root', 'bar.root', ...]
glob.glob("./*.root")
# ['./foo.root', './bar.root', ...]
...and so forth.
To get only the filename, you can use path.basename of the os module, something like this:
from glob import glob
from os import path
pattern = "/home/usr/dir/*.root"
files = [path.basename(x) for x in glob(pattern)]
# ['foo.root', 'bar.root', ...]
...or, if you want to prepend the dir part:
pattern = "/home/usr/dir/*.root"
files = [path.join('dir', path.basename(x)) for x in glob(pattern)]
# ['dir/foo.root', 'dir/bar.root', ...]
...or, if you really want the path separator at the start:
from glob import glob
import os
pattern = "/home/usr/dir/*.root"
files = [os.sep + os.path.join('dir', os.path.basename(x)) for x in glob(pattern)]
# ['/dir/foo.root', '/dir/bar.root', ...]
Using path.join and path.sep will make sure that the correct path syntax is used, depending on your OS (i.e. / or \ as a separator).
Depending on what you are really trying to do here, you might want to look at os.path.relpath, for the relative path. The title of your question indicates that relative paths might be what you are actually after:
pattern = "/home/usr/dir/*.root"
files = [os.path.relpath(x) for x in glob(pattern)]
# files will now contain the relative path to each file, from the current working directory
just use glob for getting the list you want
and then use os.path.relpath on each file
import glob
files_names = []
for file in glob.glob('/home/usr/dir/*.root'):
files_names.append(os.path.relpath(file, "/home/usr"))
You can also use regex
import re
files_names.append(re.sub(r'//home//usr//','', file, flags=re.I))
Instead of glob(pathname), use the glob1(dirname, pattern) method 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',
...]
Try this
os.path.basename(A[i])
this will return the filename alone.
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"
There are a couple of ways:
pathlib.Path().rglob()
Use pathlib.Path().rglob() from the pathlib module, which was introduced in Python 3.5.
from pathlib import Path
for path in Path('src').rglob('*.c'):
print(path.name)
glob.glob()
If you don't want to use pathlib, use glob.glob():
from glob import glob
for filename in glob('src/**/*.c', recursive=True):
print(filename)
For cases where matching files beginning with a dot (.); like files in the current directory or hidden files on Unix based system, use the os.walk() solution below.
os.walk()
For older Python versions, use os.walk() to recursively walk a directory and fnmatch.filter() to match against a simple expression:
import fnmatch
import os
matches = []
for root, dirnames, filenames in os.walk('src'):
for filename in fnmatch.filter(filenames, '*.c'):
matches.append(os.path.join(root, filename))
This version should also be faster depending on how many files you have, as the pathlib module has a bit of overhead over os.walk().
For python >= 3.5 you can use **, recursive=True, i.e.:
import glob
for f in glob.glob('/path/**/*.c', recursive=True):
print(f)
If recursive is
True(defaultFalse), the pattern**will match any files and zero or moredirectoriesandsubdirectories. If the pattern is followed by anos.sep, only directories andsubdirectoriesmatch.
Python 3 Demo