C.Radford
C.Radford

Reputation: 932

Python get full path for files in folder passed as argument

My goal is to be able to parse through a folder containing multiples images and get the absolute path for them so I can load them into OpenCV.

A single folder is passed in containing an unknown number of images.

#FilePass.py

import os
import cv2
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-1", "--first", required=True)
args = vars(ap.parse_args())
imagesToStitch = os.listdir(args["first"])
for image in imagesToStitch:
    print os.path.abspath(image)

This is the command to execute with the file path to the folder being used.

# python FilePass.py --first C:\Users\cradford\Documents\Research\ImagesToPass\Pass1

The images contained are and their corresponding full file path are:

_001.jpg # "C:\Users\cradford\Documents\Research\ImagesToPass\Pass1"
_002.jpg # "C:\Users\cradford\Documents\Research\ImagesToPass\Pass1"

Desired output:

C:\Users\cradford\Documents\Research\ImagesToPass\Pass1\_001.jpg
C:\Users\cradford\Documents\Research\ImagesToPass\Pass1\_002.jpg

Output with code used:

C:\Users\cradford\Documents\Research\OpenCV Practice Files\_001.jpg
C:\Users\cradford\Documents\Research\OpenCV Practice Files\_002.jpg

I am pretty sure the discrepancy where the result reads "OpenCV Practice Files" is because it is looking at the abs.path of "image" in my for loop and the code file is saved in "OpenCV Practice Files". Any help is appreciated.

Upvotes: 3

Views: 6567

Answers (1)

joebeeson
joebeeson

Reputation: 4366

You'll likely want to use os.path.join

for image in imagesToStitch:
    print os.path.join(args["first"], image)

Upvotes: 3

Related Questions