Trying to read an mp3 file in ruby - ruby-on-rails

I am looking a read an mp3 file currently located in my public folder.
I have tried the following:
2.5.3 :016 > audio_file = File.new('/demo.mp3', 'rb')
And it resulted with the following error:
Traceback (most recent call last):
3: from (irb):16
2: from (irb):16:in `new'
1: from (irb):16:in `initialize'
Errno::ENOENT (No such file or directory # rb_sysopen - /demo.mp3)
I would assume this is pretty straight forward but for some reason it does not work. Does anybody knows how to do that ?
Alternatively, would it be possible to read a file that is located in a server via an http://xxxxxxx.xxxxx/xxxx/mp3 for example ? (This is where the file is located initially).

You've got the wrong path. /demo.mp3 will look for the file at the root of your file system.
If it's in public/, then you need to specify the path using the relative path public/demo.mp3 since the app is running from the project folder (supposedly) which contains the public/ folder.

You can also use path "#{Rails.root}" + "/public/demo.mp3"

Related

Getting Errno::ENOENT (No such file or directory # rb_sysopen) while trying to - File.open('condoleads1.json')

I'm trying to open a JSON file (using ruby on rails) that's on my computer titled "condoleads1.json". When I run the command - File.open('condoleads1.json'),I get the following error :Getting Errno::ENOENT (No such file or directory # rb_sysopen)
The file has to be in the directory where you run the program (formally, the working directory), not just anywhere on your computer. If you really do need to access a file that's somewhere else, use its full path (like C:\Users\the\the.txt or /home/the/the.txt) and not just the name

No such file or directory in VScode

Does anyone has a solution for this error?ยด
dan#LAPTOP-U61VD2F3:/mnt/c/Users/danyl/Desktop/New folder$ ruby "c:\Users\danyl\Desktop\New folder\Lesson.rb"
Traceback (most recent call last):
ruby: No such file or directory -- c:\\Users\\danyl\\Desktop\\New folder\\Lesson.rb (LoadError)
dan#LAPTOP-U61VD2F3:/mnt/c/Users/danyl/Desktop/New folder$
Solution in this case:
Go to your settings.json file in Vscode and add this line into the settings:
"code-runner.terminalRoot": "/mnt/",
or you can open settings and find Code-runner: Terminal Root and type this:
/mnt/
Note: For Windows system, replaces the Windows style drive letter in the command with a Unix style root when using a custom shell as the terminal, like Bash or Cgywin. Example: Setting this to '/mnt/' will replace 'C:\path' with '/mnt/c/path'

Trying to understand and download file with SFTP.download in Ruby/Rails

I'm trying to download a file into a Rails application using the SFTP method with sftp.download!('path/to/remote/folder/#{filename}', 'path/to/local/folder)
The full methods:
def missouri
sftp = Net::SFTP.start('host.name', '<username>', password: '<password>')
sftp.dir.entries('/path/to/remote/folder/') do |entry|
sftp.download!('/path/to/remote/folder/#{entry.name}.txt', '/Users/<me>/local/folder/')
end
end
I get an error:
RuntimeError (open /path/to/remote/folder/filename.txt: no such file (2))
If I take away the file extension (txt), I get the error:
Errno::EACCES (Permission denied # rb_sysopen - /Users/<me>/local/folder/)
When I 'drill' down to that part and run this method, I get a return of the NET::Sftp object with all of it's attributes but no actual download. The SFTP I'm trying to access is an ASPX.human page and the path I'm trying to download the file is within the file structure of the remote site so I'm not sure if I'm trying to access the wrong path?
If I go to the actual SFTP, I can clearly see a file sitting there.
The comment you added after your question makes we wonder about unix rights. Is your script launched by a user that is allowed to write on the destination local directory?
Said differently, what is the output of the following shell command?
ls -l /path/to/remote/folder
And what are the user/group used your script?
By the way, one thing strange in your code:
sftp.download!('/path/to/remote/folder/#{entry.name}.txt'
should be:
sftp.download!("/path/to/remote/folder/#{entry.name}.txt"
Otherwise entry.name would not be replaced.

Lua Relative Path Required

I am writing a Lua script and am trying to use require on a file that exists in the same directory as the main script. I cannot seem to get require to work in this case and have tried several solutions I have found but none seem to work. I have the following files together in a directory:
main.lua
helper.lua
I've tried the following solutions and gotten the error following each:
Solution 1:
local folderOfThisFile = (...):match("(.-)[^%.]+$")
local helper = require(folderOfThisFile .. 'helper')
lua: ...domizerWPF\DataFiles\LUA\main.lua:2: attempt to index local 'pathOfThisFile' (a nil value)
stack traceback:
...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
[C]: ?
Solution 2:
package.path = "/?.lua;" .. package.path
local helper = require('helper')
lua: ...domizerWPF\DataFiles\LUA\main.lua:2: module 'helper' not found:
no field package.preload['helper']
no file '/helper.lua'
no file '.\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\helper.lua'
no file 'C:\Program Files (x86)\Lua\5.1\helper\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.luac'
no file '.\helper.dll'
no file '.\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\helper.dll'
no file 'C:\Program Files (x86)\Lua\5.1\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper51.dll'
no file 'C:\Program Files (x86)\Lua\5.1\loadall.dll'
no file 'C:\Program Files (x86)\Lua\5.1\clibs\loadall.dll'
stack traceback:
[C]: in function 'require'
...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
[C]: ?
I've tried variations on Solution 2 with various paths such as "?.lua;" and "./?.lua;" to no avail.
These two lines of the error message shed some light on your problem:
no file '/helper.lua'
no file '.\helper.lua'
The first line is due to your change to package.path. As you can see, it looks for a "/helper.lua" file that doesn't exist so its not doing anything. The second line is due to the default package.path and is looking for a "helper.lua" in the current working directory. Since its not finding, your current working directory must not be the directory your main.lua is located on.
The fix is to either make the current working directory the directory where main.lua and helper.lua are located or to add "C:\\path\\to\\your\\lua\\project\\?.lua" to the package.path
i am still learning lua but here is what I can whip up for you,
if you don't have a file system API installed then you can make a string variable with your curent working dir in it and you can add to it like this
local cwd="C:\users\user\Desktop\"
dofile(cwd.."program.lua")
thats what I do, and I have no problems with it
If you mean that you want to be able to invoke the program from any directory and that it correctly locates the required files, then you can use this solution (you only need it within main.lua):
local base_path = string.match(arg[0], '^(.-)[^/\\]*$')
package.path = string.format("%s;%s?.lua", package.path, base_path)
This works by adding the directory where the file is to the package path, so that require can work on the files in that directory. Lua doesn't do this automatically yet (Python does, since version 2.6 or so); hopefully it will be implemented in future. You can also use base_path to refer to other files in the same directory. In my case, for example, there is a SQLite database in that directory and the program needs to open it, so I use this:
local database_filename = base_path .. 'db.sqlite'
You can also make base_path a global so that it's available to other modules if necessary.

Biopython cannot find file

I am trying to run a qblast from the Python prompt and after importing all the libraries I need, Python cannot find my file:
>>> record = SeqIO.read(open("sinchimeras_1.fasta"), format="fasta")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'sinchimeras_1.fasta'
I have tried to write down all the route for the file ("/Users/imac...") and to move the file to the Python and to the Biopython folders, and I get the same message.
Where do I have to save my file? What am I doing wrong?
You need to move that file to your working directory or use an absolute path.
This problem is independent of Biopython; the IOError is coming from open("sinchimeras_1.fasta").
Explanation
When you provide Python the relative path "sinchimeras_1.fasta", it looks in the current directory for such a file. So, instead of moving your Fasta file to a Python/Biopython folder, ensure it's in your working directory (os.getcwd() may be helpful).
Alternatively, you can supply the absolute path to the file in place of "sinchimeras_1.fasta" (e.g. open("/Users/imac.../sinchimeras_1.fasta")).

Resources