lbug
lbug

Reputation: 371

Getting (and appending) correct file path in Python 2.7

I have a previously defined file name in a string format, and a previously defined variable called value. I am trying to store a variable that looks like:

C:\Users\Me\Desktop\Value_Validation_Report

with the syntax below, I instead get:

C:\Users\Me\Desktop\Value\ _Validation_Report

target_dir= os.path.dirname(os.path.realpath(FileName))
ValidationReport=os.path.join(target_dir,value,"_Validation_Report")
print ValidationReport

Every other combination I have tried leads to an error. Any help would be greatly appreciated!

Upvotes: 2

Views: 92

Answers (1)

Jamie Counsell
Jamie Counsell

Reputation: 8143

If value is a String, you must concatenate that with "_Validation_Report

target_dir= os.path.dirname(os.path.realpath(FileName))
ValidationReport=os.path.join(target_dir,value + "_Validation_Report")
print ValidationReport

os.path.join will add a separator (which depends on the operating system) between each string you give it. To avoid this, simply put your value and "_Validation_Report" strings together as one String. See more about os.path.join.

Upvotes: 2

Related Questions