Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.open, write and save?

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.

like image 490
ChuckJHardy Avatar asked Feb 15 '11 20:02

ChuckJHardy


People also ask

How do I open and save a file in Python?

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.

How do you open a file for both reading and writing?

'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.

Where does Python save files?

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.


1 Answers

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 
like image 83
Simone Carletti Avatar answered Sep 22 '22 14:09

Simone Carletti