# Best way to access directory relational to current file

**URL:** <https://www.ruby-forum.com/t/best-way-to-access-directory-relational-to-current-file/83645>\
**Category:** Ruby\
**Created:** [January 28, 2007, 7:56am UTC](https://www.ruby-forum.com/t/best-way-to-access-directory-relational-to-current-file/83645 "2007-01-28T07:56:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [January 28, 2007, 7:56am UTC](https://www.ruby-forum.com/t/best-way-to-access-directory-relational-to-current-file/83645/1 "2007-01-28T07:56:28Z")

</div>

I’m trying to find the full path to a file. File.dirname( **FILE** )  
always returns “.”, which doesn’t really do me any good. The working  
directory is not always the same as where the file is, so I can’t use  
the “.” in any useful way.

What I’m trying to do is locate where a particular file is because I  
know where other files are in relation to this particular file. I  
don’t know what the working directory will be though, anyone could  
execute the ruby file originating from any location.

Thanks for any thoughts…

phil

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [January 28, 2007, 8:01am UTC](https://www.ruby-forum.com/t/best-way-to-access-directory-relational-to-current-file/83645/2 "2007-01-28T08:01:28Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’m trying to find the full path to a file. File.dirname( **FILE** )  
> always returns “.”, which doesn’t really do me any good. The working  
> directory is not always the same as where the file is, so I can’t use  
> the “.” in any useful way.

I think you want File.expand\_path(File.dirname( **FILE** ))

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [January 28, 2007, 2:29pm UTC](https://www.ruby-forum.com/t/best-way-to-access-directory-relational-to-current-file/83645/3 "2007-01-28T14:29:28Z")

</div>

Daniel B. wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > I’m trying to find the full path to a file. File.dirname( **FILE** )  
> > always returns “.”, which doesn’t really do me any good. The working  
> > directory is not always the same as where the file is, so I can’t use  
> > the “.” in any useful way.
> 
> I think you want File.expand\_path(File.dirname( **FILE** ))

I tend to use this idiom a lot:  
require File.expand\_path( **FILE** +’/…/relative/path/to/file’)

For ruby, “file” and “dummydir/…/file” are considered as 2 different  
files, so I think it’s a good idea to always use absolute directories  
when doing a require.

Daniel
