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.



enter image description here



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?










share|improve this question


























    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.



    enter image description here



    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?










    share|improve this question
























      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.



      enter image description here



      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?










      share|improve this question













      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.



      enter image description here



      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 16:55









      Austin Burke

      4016




      4016
























          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






          share|improve this answer





















          • 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 the Rails.root.join function works?
            – Austin Burke
            Nov 8 at 19:11


















          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")





          share|improve this answer





















          • 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










          • 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










          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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






          share|improve this answer





















          • 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 the Rails.root.join function works?
            – Austin Burke
            Nov 8 at 19:11















          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






          share|improve this answer





















          • 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 the Rails.root.join function works?
            – Austin Burke
            Nov 8 at 19:11













          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






          share|improve this answer












          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 the Rails.root.join function 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










          • 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 the Rails.root.join function 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












          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")





          share|improve this answer





















          • 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










          • 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










          • 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















          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")





          share|improve this answer





















          • 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










          • 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










          • 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













          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")





          share|improve this answer












          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")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 17:13









          SteveTurczyn

          25.6k42738




          25.6k42738












          • 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










          • 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










          • 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


















          • 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










          • 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










          • 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
















          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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff