I am trying to get a .rb file to make another .rb file within a specific directory with specified content, when that file is run. I dont know whether the best way to do this would be with a Ruby file or a Rake file. You input would be great.
To open a file, you need to use the built-in open function. The Python file open function returns a file object that contains methods and attributes to perform various operations for opening files in Python. Here, filename: gives name of the file that the file object has opened.
'r+' opens the file for both reading and writing. On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Also reading then writing works equally well using 'r+b' mode, but you have to use f.
Right-click the Python window and select Save As to save your code either as a Python file (. py) or Text file (. txt). If saving to a Python file, only the Python code will be saved.
If you just need to perform a simple script like creating a file, you can simply use a Ruby script without creating a rake task.
# file origin.rb target = "target.rb" content = <<-RUBY puts "I'm the target!" RUBY File.open(target, "w+") do |f| f.write(content) end
And you can execute the file with
$ ruby origin.rb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With