File location with variable attribute

45 views Asked by At

I want to attach the newly created csv file for emailing from inside a program. The filename is generated inside the program and stored as fname (a string). How do I use that to mention the file location?

Will this work?

def emailing(fname, attachment)
    ...
    ...
    attachment = open(/home/pi/Adafruit_Python_MAX31855/%s, fname)
    ...
2

There are 2 answers

1
penta On

You should use, '{}'.format() right after the location.

attachment = open('/home/pi/Adafruit_Python_MAX31855/{}'.format(fname))

To know More about .format refer here. https://pyformat.info/

I suggest you learn basics of python first, before trying.

0
furas On

Use special function to concatenate path

open( os.path.join("long/path/", fname) )

Python doc: os.path.join

Eventually concatenate strings

open( "long/path/" + fname )
open( "long/path/%s" % fname )
open( "long/path/{}".format(fname) )