M. Bedross
M. Bedross

Reputation: 129

Storing filepath as variable in python

I'm sure this is an easy question but I have searched for a while to no avail. I want to define a file path as a variable and use that variable elsewhere in my python code on Ubuntu. What I have so far is:

filefolder = '/home/Desktop/Sample Loading'

and I call on it later in the code as:

file = open('%f/EventLog.txt' % (filefolder), "a")

When I do this, I get an error saying that a float was expected and not a string. How can I get this to work?

Thanks in advance!

Upvotes: 0

Views: 27949

Answers (2)

Juxhin
Juxhin

Reputation: 5630

You passed a formatter that expected a float (%f) not a string (%s).

You can either replace this with %s/Event log.txt or just concatenate it directly like so, filefolder + '/Event log.txt.

Do note that you're better off working with os module directly for essentially anything to do with paths :-)

Will edit answer if you require further explanation as II sent this from phone.

Upvotes: 2

gt6989b
gt6989b

Reputation: 4233

use file = open('%s/EventLog.txt' % (filefolder), "a") with %s instead of %f

But you are much better off using os.path.join(filefolder, 'EventLog.txt')

Upvotes: 3

Related Questions