Get path of filename

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ivo
Prole
Posts: 7
Joined: Sat Apr 05, 2014 10:05 pm
Contact:

Get path of filename

Post by Ivo »

I want to write a function to get the path of a filename in a LÖVE program, and am finding it difficult. getDir('/usr/local/bin/sl') should return '/usr/local/bin'. I essentially want to remove the last item in a filename. In python I would just do:

Code: Select all

def getDir(sPath):
  dir = '/'.join(sPath.split('/')[:-1])
  return dir
but as far as I know lua does not have the split or the join functions.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Get path of filename

Post by slime »

Something like this:

Code: Select all

function getDir(filepath)
    return filepath:match("(.+)/") or ""
end
It will return a string containing everything up to the last '/' in the filepath, or an empty string if there isn't any '/'.
User avatar
Ivo
Prole
Posts: 7
Joined: Sat Apr 05, 2014 10:05 pm
Contact:

Re: Get path of filename

Post by Ivo »

worked perfectly, thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 82 guests