-1

How to get result exactly the same format as follows?

result = ( C:\data\a.jpg C:\data\b.jpg C:\data\c.jpg )

The following code fails:

import glob
files = glob.glob ('*.jpg')
for file in files:
  result = "C:\data\" + file
jh314
  • 27,144
  • 16
  • 62
  • 82

2 Answers2

1
import os, glob
files = glob.glob('*.jpg')
files = [os.path.join("C:\\data", file) for file in files]
result = "( " + " ".join(files) + " )"
print result  # Prints ( C:\data\a.jpg C:\data\b.jpg C:\data\c.jpg )

(You might want to use os.getcwd() rather than the literal "C:\\data".)

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
0

You can also try to split the back slash as well like this:

person_names.add(image_path_names[-1].split('/')[0].split('\\')[1])