Skip to content

Instantly share code, notes, and snippets.

@sebastiz
Forked from coetry/gist:6e110092eb0943ddc0d4
Last active January 18, 2016 16:17
Show Gist options
  • Save sebastiz/d4cd7e035038e946571b to your computer and use it in GitHub Desktop.
Save sebastiz/d4cd7e035038e946571b to your computer and use it in GitHub Desktop.
File Commands in Ruby
File.open "file.json", "w+" do |file|
file.puts Hash.to_json
end
#This writes a whole string to a file (string is the output of method mypage.makePage)
File.open("/Users/sebastianzeki/Desktop/MyRubyHTML.html", 'w') { |file| file.write(mypage.makePage)}
File.open("/path/to/file").each { |line|
puts line
}
File.open(filename, 'r') { |f| f.read }
file = File.open("<file>", "rb").read
#Read file and select only certain lines eg where /ohn/ is a regex for ohn
File.readlines("path/to/file.txt").select { |line| line =~ /ohn/ }
#Create a new file
File.open("out.txt", "w+") {|f| f.write("write your stuff here") }
where your options are:
r - Read only. The file must exist.
w - Create an empty file for writing.
a - Append to a file.The file is created if it does not exist.
r+ - Open a file for update both reading and writing. The file must exist.
w+ - Create an empty file for both reading and writing.
a+ - Open a file for reading and appending. The file is created if it does not exist.
In your case, w is preferable.
OR you could have:
out_file = File.new("out.txt", "w")
#...
out_file.puts("write your stuff here")
#...
out_file.close
#Open and read a CSV file
require 'csv'
CSV.open("/tmp/file.csv", "wb") do |csv|
str.scan(/^(SLX-.*?)(?=\R+SLX-)/m).map do |s| # break by SLX-
s.first.split($/).map do |el| # split by CR
"\"#{el}\"" # quote values
end
end.each do |line| # iterate
csv << line # fulfil csv The << appends to the end of the text file
end
end
File Inquiries:
The following command tests whether a file exists before opening it:
#!/usr/bin/ruby
File.open("file.rb") if File::exists?( "file.rb" )
The following command inquire whether the file is really a file:
#!/usr/bin/ruby
# This returns either true or false
File.file?( "text.txt" )
The following command finds out if it given file name is a directory:
#!/usr/bin/ruby
# a directory
File::directory?( "/usr/local/bin" ) # => true
# a file
File::directory?( "file.rb" ) # => false
The following command finds whether the file is readable, writable or executable:
#!/usr/bin/ruby
File.readable?( "test.txt" ) # => true
File.writable?( "test.txt" ) # => true
File.executable?( "test.txt" ) # => false
The following command finds whether the file has zero size or not:
#!/usr/bin/ruby
File.zero?( "test.txt" ) # => true
The following command returns size of the file :
#!/usr/bin/ruby
File.size?( "text.txt" ) # => 1002
The following command can be used to find out a type of file :
#!/usr/bin/ruby
File::ftype( "test.txt" ) # => file
The ftype method identifies the type of the file by returning one of the following: file, directory, characterSpecial, blockSpecial, fifo, link, socket, or unknown.
The following command can be used to find when a file was created, modified, or last accessed :
#!/usr/bin/ruby
File::ctime( "test.txt" ) # => Fri May 09 10:06:37 -0700 2008
File::mtime( "text.txt" ) # => Fri May 09 10:44:44 -0700 2008
File::atime( "text.txt" ) # => Fri May 09 10:45:01 -0700 2008
#Dictionary lookup find and replace filenames
require 'csv'
#Alternative way to get the columns
csv_data = CSV.read '/Users/sebastianzeki/Desktop/tbb.csv'
#Create a hash from the two rows (they have to be organised as rows here) in the csv
h3=Hash[*csv_data.flatten]
#Create array from just the keys in the csv
arr=h3.keys
#Create dummy text files in a folder from the array
arr.map {|f| File.open("/Users/sebastianzeki/Desktop/TestDummyFilesTBB/"+f, 'w+')}
csv_data.each do |old, new|
File.rename(path + old, path + new)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment