-1

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)
    ...
martineau
  • 119,623
  • 25
  • 170
  • 301
Dr.Viper
  • 411
  • 2
  • 5
  • 17
  • 3
    first try, later ask. – furas Dec 22 '16 at 08:10
  • concatenate strings `"long/path/" + fname` or `"long/path/%s" % fname` or `"long/path/{}".format(fname)` or use special functions to concatenate path `os.path.join("long/path/", fname)` – furas Dec 22 '16 at 08:12

2 Answers2

1

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) )
furas
  • 134,197
  • 12
  • 106
  • 148
0

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.

penta
  • 2,536
  • 4
  • 25
  • 50
  • For file and folder paths, it's better to use the `os.path` utility functions because they make it easy to write code which is portable among OSs. – martineau Dec 22 '16 at 09:38