Ruby - File.open(text_file.txt) is not able to find the file?
up vote
0
down vote
favorite
This is probably a simple "Navigating directory" problem since I'm new but..
Inside of a file named tasks_lists.rb I am attempting to save the text inside of a text file to a variable named draft. 
Here is that code:
class FivePoints::Tasks
 attr_accessor :draft, :efile, :serve
  def self.start
    binding.pry
    draft = File.open("./text_files/draft.txt")
    efile = File.open("./text_files/efile.txt")
    serve = File.open("./text_files/serve.txt")
    self.new(draft, efile, serve)
  end
  def initialize(draft , efile, serve)
    @draft = draft
    @efile = efile
    @serve = serve
  end
end
I have tried to pry around, but no luck. I have also tried variations such as ("../text_files/efile.txt"), ("/text_files/efile.txt"). I will attach an image of the directory tree as well. 

I am simply trying to eventually puts out some text in the CLI for a sample program. That text will be in the draft.txt file. Any ideas?
ruby-on-rails ruby
add a comment |
up vote
0
down vote
favorite
This is probably a simple "Navigating directory" problem since I'm new but..
Inside of a file named tasks_lists.rb I am attempting to save the text inside of a text file to a variable named draft. 
Here is that code:
class FivePoints::Tasks
 attr_accessor :draft, :efile, :serve
  def self.start
    binding.pry
    draft = File.open("./text_files/draft.txt")
    efile = File.open("./text_files/efile.txt")
    serve = File.open("./text_files/serve.txt")
    self.new(draft, efile, serve)
  end
  def initialize(draft , efile, serve)
    @draft = draft
    @efile = efile
    @serve = serve
  end
end
I have tried to pry around, but no luck. I have also tried variations such as ("../text_files/efile.txt"), ("/text_files/efile.txt"). I will attach an image of the directory tree as well. 

I am simply trying to eventually puts out some text in the CLI for a sample program. That text will be in the draft.txt file. Any ideas?
ruby-on-rails ruby
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is probably a simple "Navigating directory" problem since I'm new but..
Inside of a file named tasks_lists.rb I am attempting to save the text inside of a text file to a variable named draft. 
Here is that code:
class FivePoints::Tasks
 attr_accessor :draft, :efile, :serve
  def self.start
    binding.pry
    draft = File.open("./text_files/draft.txt")
    efile = File.open("./text_files/efile.txt")
    serve = File.open("./text_files/serve.txt")
    self.new(draft, efile, serve)
  end
  def initialize(draft , efile, serve)
    @draft = draft
    @efile = efile
    @serve = serve
  end
end
I have tried to pry around, but no luck. I have also tried variations such as ("../text_files/efile.txt"), ("/text_files/efile.txt"). I will attach an image of the directory tree as well. 

I am simply trying to eventually puts out some text in the CLI for a sample program. That text will be in the draft.txt file. Any ideas?
ruby-on-rails ruby
This is probably a simple "Navigating directory" problem since I'm new but..
Inside of a file named tasks_lists.rb I am attempting to save the text inside of a text file to a variable named draft. 
Here is that code:
class FivePoints::Tasks
 attr_accessor :draft, :efile, :serve
  def self.start
    binding.pry
    draft = File.open("./text_files/draft.txt")
    efile = File.open("./text_files/efile.txt")
    serve = File.open("./text_files/serve.txt")
    self.new(draft, efile, serve)
  end
  def initialize(draft , efile, serve)
    @draft = draft
    @efile = efile
    @serve = serve
  end
end
I have tried to pry around, but no luck. I have also tried variations such as ("../text_files/efile.txt"), ("/text_files/efile.txt"). I will attach an image of the directory tree as well. 

I am simply trying to eventually puts out some text in the CLI for a sample program. That text will be in the draft.txt file. Any ideas?
ruby-on-rails ruby
ruby-on-rails ruby
asked Nov 8 at 16:55
Austin Burke
4016
4016
add a comment |
add a comment |
                                2 Answers
                                2
                        
active
oldest
votes
up vote
0
down vote
Assuming you only want to run this locally, then find out the path of the file (you can view its properties from file explorer), and use that explicitly in your code.
Find the exact path (If you're on UNIX it'll be something like /home/USER/projects/five_points/bla....) then replace your string with the exact full path and you'll be able to open the files as needed
Should it be able to call on the file without using the exact path?
– Austin Burke
Nov 8 at 17:10
The only difference I can find between my program and a simple one like link is that mine are not on the same level in the directory?
– Austin Burke
Nov 8 at 17:11
1
If you have set up your Rails app to properly account for the path, then you should be able to call Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt')
– Mark
Nov 8 at 17:15
1
If you really want to avoid using the full path, open up a console. and see what Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt') returns. Keep on playing with that until it matches your local path exactly.
– Mark
Nov 8 at 17:20
I've never used Rails with this. The interface is currently just the CLI. I created the default files by Bundle gem <file_name> and then I've only added files. How can I make it so theRails.root.joinfunction works?
– Austin Burke
Nov 8 at 19:11
add a comment |
up vote
0
down vote
It looks like your files are in a directory called five_points but your code is under lib so one level higher ?
Also, you need to be sure you open the file for appending.
draft = File.open("./five_points/text_files/draft.txt", "a")
The active file is actually inlibas well. It istasks_lists.rbin thelistsfolder. So there should be no need to go intofive_points
– Austin Burke
Nov 8 at 19:17
Your example in your code you're trying to accessdraft.txtwhich is undertext_fileswhich is underfive_pointswhich is underlib. Are you saying the code snipped you inserted is fromtaaks_lists.rband notfive_points.rb? If it's called fromfive_points.rbthen the start position is still beforefive_points.
– SteveTurczyn
Nov 9 at 8:12
Yes, the code snippet is from ‘tasks_lists.rb’
– Austin Burke
Nov 9 at 16:39
And is it loaded intofive_points.rbto be executed?
– SteveTurczyn
Nov 9 at 17:35
At thebinding.pryin thestartmethod, type inDir.pwdand see what the working directory is.
– SteveTurczyn
Nov 9 at 17:38
|
show 3 more comments
                                2 Answers
                                2
                        
active
oldest
votes
                                2 Answers
                                2
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Assuming you only want to run this locally, then find out the path of the file (you can view its properties from file explorer), and use that explicitly in your code.
Find the exact path (If you're on UNIX it'll be something like /home/USER/projects/five_points/bla....) then replace your string with the exact full path and you'll be able to open the files as needed
Should it be able to call on the file without using the exact path?
– Austin Burke
Nov 8 at 17:10
The only difference I can find between my program and a simple one like link is that mine are not on the same level in the directory?
– Austin Burke
Nov 8 at 17:11
1
If you have set up your Rails app to properly account for the path, then you should be able to call Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt')
– Mark
Nov 8 at 17:15
1
If you really want to avoid using the full path, open up a console. and see what Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt') returns. Keep on playing with that until it matches your local path exactly.
– Mark
Nov 8 at 17:20
I've never used Rails with this. The interface is currently just the CLI. I created the default files by Bundle gem <file_name> and then I've only added files. How can I make it so theRails.root.joinfunction works?
– Austin Burke
Nov 8 at 19:11
add a comment |
up vote
0
down vote
Assuming you only want to run this locally, then find out the path of the file (you can view its properties from file explorer), and use that explicitly in your code.
Find the exact path (If you're on UNIX it'll be something like /home/USER/projects/five_points/bla....) then replace your string with the exact full path and you'll be able to open the files as needed
Should it be able to call on the file without using the exact path?
– Austin Burke
Nov 8 at 17:10
The only difference I can find between my program and a simple one like link is that mine are not on the same level in the directory?
– Austin Burke
Nov 8 at 17:11
1
If you have set up your Rails app to properly account for the path, then you should be able to call Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt')
– Mark
Nov 8 at 17:15
1
If you really want to avoid using the full path, open up a console. and see what Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt') returns. Keep on playing with that until it matches your local path exactly.
– Mark
Nov 8 at 17:20
I've never used Rails with this. The interface is currently just the CLI. I created the default files by Bundle gem <file_name> and then I've only added files. How can I make it so theRails.root.joinfunction works?
– Austin Burke
Nov 8 at 19:11
add a comment |
up vote
0
down vote
up vote
0
down vote
Assuming you only want to run this locally, then find out the path of the file (you can view its properties from file explorer), and use that explicitly in your code.
Find the exact path (If you're on UNIX it'll be something like /home/USER/projects/five_points/bla....) then replace your string with the exact full path and you'll be able to open the files as needed
Assuming you only want to run this locally, then find out the path of the file (you can view its properties from file explorer), and use that explicitly in your code.
Find the exact path (If you're on UNIX it'll be something like /home/USER/projects/five_points/bla....) then replace your string with the exact full path and you'll be able to open the files as needed
answered Nov 8 at 17:04
Mark
1,4731522
1,4731522
Should it be able to call on the file without using the exact path?
– Austin Burke
Nov 8 at 17:10
The only difference I can find between my program and a simple one like link is that mine are not on the same level in the directory?
– Austin Burke
Nov 8 at 17:11
1
If you have set up your Rails app to properly account for the path, then you should be able to call Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt')
– Mark
Nov 8 at 17:15
1
If you really want to avoid using the full path, open up a console. and see what Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt') returns. Keep on playing with that until it matches your local path exactly.
– Mark
Nov 8 at 17:20
I've never used Rails with this. The interface is currently just the CLI. I created the default files by Bundle gem <file_name> and then I've only added files. How can I make it so theRails.root.joinfunction works?
– Austin Burke
Nov 8 at 19:11
add a comment |
Should it be able to call on the file without using the exact path?
– Austin Burke
Nov 8 at 17:10
The only difference I can find between my program and a simple one like link is that mine are not on the same level in the directory?
– Austin Burke
Nov 8 at 17:11
1
If you have set up your Rails app to properly account for the path, then you should be able to call Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt')
– Mark
Nov 8 at 17:15
1
If you really want to avoid using the full path, open up a console. and see what Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt') returns. Keep on playing with that until it matches your local path exactly.
– Mark
Nov 8 at 17:20
I've never used Rails with this. The interface is currently just the CLI. I created the default files by Bundle gem <file_name> and then I've only added files. How can I make it so theRails.root.joinfunction works?
– Austin Burke
Nov 8 at 19:11
Should it be able to call on the file without using the exact path?
– Austin Burke
Nov 8 at 17:10
Should it be able to call on the file without using the exact path?
– Austin Burke
Nov 8 at 17:10
The only difference I can find between my program and a simple one like link is that mine are not on the same level in the directory?
– Austin Burke
Nov 8 at 17:11
The only difference I can find between my program and a simple one like link is that mine are not on the same level in the directory?
– Austin Burke
Nov 8 at 17:11
1
1
If you have set up your Rails app to properly account for the path, then you should be able to call Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt')
– Mark
Nov 8 at 17:15
If you have set up your Rails app to properly account for the path, then you should be able to call Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt')
– Mark
Nov 8 at 17:15
1
1
If you really want to avoid using the full path, open up a console. and see what Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt') returns. Keep on playing with that until it matches your local path exactly.
– Mark
Nov 8 at 17:20
If you really want to avoid using the full path, open up a console. and see what Rails.root.join('lib', 'five_points', 'text_files', 'draft.txt') returns. Keep on playing with that until it matches your local path exactly.
– Mark
Nov 8 at 17:20
I've never used Rails with this. The interface is currently just the CLI. I created the default files by Bundle gem <file_name> and then I've only added files. How can I make it so the
Rails.root.join function works?– Austin Burke
Nov 8 at 19:11
I've never used Rails with this. The interface is currently just the CLI. I created the default files by Bundle gem <file_name> and then I've only added files. How can I make it so the
Rails.root.join function works?– Austin Burke
Nov 8 at 19:11
add a comment |
up vote
0
down vote
It looks like your files are in a directory called five_points but your code is under lib so one level higher ?
Also, you need to be sure you open the file for appending.
draft = File.open("./five_points/text_files/draft.txt", "a")
The active file is actually inlibas well. It istasks_lists.rbin thelistsfolder. So there should be no need to go intofive_points
– Austin Burke
Nov 8 at 19:17
Your example in your code you're trying to accessdraft.txtwhich is undertext_fileswhich is underfive_pointswhich is underlib. Are you saying the code snipped you inserted is fromtaaks_lists.rband notfive_points.rb? If it's called fromfive_points.rbthen the start position is still beforefive_points.
– SteveTurczyn
Nov 9 at 8:12
Yes, the code snippet is from ‘tasks_lists.rb’
– Austin Burke
Nov 9 at 16:39
And is it loaded intofive_points.rbto be executed?
– SteveTurczyn
Nov 9 at 17:35
At thebinding.pryin thestartmethod, type inDir.pwdand see what the working directory is.
– SteveTurczyn
Nov 9 at 17:38
|
show 3 more comments
up vote
0
down vote
It looks like your files are in a directory called five_points but your code is under lib so one level higher ?
Also, you need to be sure you open the file for appending.
draft = File.open("./five_points/text_files/draft.txt", "a")
The active file is actually inlibas well. It istasks_lists.rbin thelistsfolder. So there should be no need to go intofive_points
– Austin Burke
Nov 8 at 19:17
Your example in your code you're trying to accessdraft.txtwhich is undertext_fileswhich is underfive_pointswhich is underlib. Are you saying the code snipped you inserted is fromtaaks_lists.rband notfive_points.rb? If it's called fromfive_points.rbthen the start position is still beforefive_points.
– SteveTurczyn
Nov 9 at 8:12
Yes, the code snippet is from ‘tasks_lists.rb’
– Austin Burke
Nov 9 at 16:39
And is it loaded intofive_points.rbto be executed?
– SteveTurczyn
Nov 9 at 17:35
At thebinding.pryin thestartmethod, type inDir.pwdand see what the working directory is.
– SteveTurczyn
Nov 9 at 17:38
|
show 3 more comments
up vote
0
down vote
up vote
0
down vote
It looks like your files are in a directory called five_points but your code is under lib so one level higher ?
Also, you need to be sure you open the file for appending.
draft = File.open("./five_points/text_files/draft.txt", "a")
It looks like your files are in a directory called five_points but your code is under lib so one level higher ?
Also, you need to be sure you open the file for appending.
draft = File.open("./five_points/text_files/draft.txt", "a")
answered Nov 8 at 17:13
SteveTurczyn
25.6k42738
25.6k42738
The active file is actually inlibas well. It istasks_lists.rbin thelistsfolder. So there should be no need to go intofive_points
– Austin Burke
Nov 8 at 19:17
Your example in your code you're trying to accessdraft.txtwhich is undertext_fileswhich is underfive_pointswhich is underlib. Are you saying the code snipped you inserted is fromtaaks_lists.rband notfive_points.rb? If it's called fromfive_points.rbthen the start position is still beforefive_points.
– SteveTurczyn
Nov 9 at 8:12
Yes, the code snippet is from ‘tasks_lists.rb’
– Austin Burke
Nov 9 at 16:39
And is it loaded intofive_points.rbto be executed?
– SteveTurczyn
Nov 9 at 17:35
At thebinding.pryin thestartmethod, type inDir.pwdand see what the working directory is.
– SteveTurczyn
Nov 9 at 17:38
|
show 3 more comments
The active file is actually inlibas well. It istasks_lists.rbin thelistsfolder. So there should be no need to go intofive_points
– Austin Burke
Nov 8 at 19:17
Your example in your code you're trying to accessdraft.txtwhich is undertext_fileswhich is underfive_pointswhich is underlib. Are you saying the code snipped you inserted is fromtaaks_lists.rband notfive_points.rb? If it's called fromfive_points.rbthen the start position is still beforefive_points.
– SteveTurczyn
Nov 9 at 8:12
Yes, the code snippet is from ‘tasks_lists.rb’
– Austin Burke
Nov 9 at 16:39
And is it loaded intofive_points.rbto be executed?
– SteveTurczyn
Nov 9 at 17:35
At thebinding.pryin thestartmethod, type inDir.pwdand see what the working directory is.
– SteveTurczyn
Nov 9 at 17:38
The active file is actually in
lib as well. It is tasks_lists.rb in the lists folder. So there should be no need to go into five_points– Austin Burke
Nov 8 at 19:17
The active file is actually in
lib as well. It is tasks_lists.rb in the lists folder. So there should be no need to go into five_points– Austin Burke
Nov 8 at 19:17
Your example in your code you're trying to access
draft.txt which is under text_files which is under five_points which is under lib. Are you saying the code snipped you inserted is from taaks_lists.rb and not five_points.rb?  If it's called from five_points.rb then the start position is still before five_points.– SteveTurczyn
Nov 9 at 8:12
Your example in your code you're trying to access
draft.txt which is under text_files which is under five_points which is under lib. Are you saying the code snipped you inserted is from taaks_lists.rb and not five_points.rb?  If it's called from five_points.rb then the start position is still before five_points.– SteveTurczyn
Nov 9 at 8:12
Yes, the code snippet is from ‘tasks_lists.rb’
– Austin Burke
Nov 9 at 16:39
Yes, the code snippet is from ‘tasks_lists.rb’
– Austin Burke
Nov 9 at 16:39
And is it loaded into
five_points.rb to be executed?– SteveTurczyn
Nov 9 at 17:35
And is it loaded into
five_points.rb to be executed?– SteveTurczyn
Nov 9 at 17:35
At the
binding.pry in the start method, type in Dir.pwd and see what the working directory is.– SteveTurczyn
Nov 9 at 17:38
At the
binding.pry in the start method, type in Dir.pwd and see what the working directory is.– SteveTurczyn
Nov 9 at 17:38
|
show 3 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212552%2fruby-file-opentext-file-txt-is-not-able-to-find-the-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown