File System input and output with ruby
print and puts statementsgets statementchomp to remove line return: input = gets.chomp/\#using the File class:
File.join("users", "documents", "me.rb")
#output is
# users/documents/me.rb for mac and
# users\documents\me.rb for windows
#to get an absolute path (from the root), first arg is empty string
File.join("","...", "...")
chmod
chown
absolute path: path from the root folder to the file, begins with a forward slash
relative path: path from where we are now to where the file lives
ruby gives us a variable that gives us the path to the file we are in now: _FILE_
File.expand_path(_FILE_)File.dirname(_FILE_)to get the name of the file: puts _FILE_ => relative path
file = File.new(file_name, mode_to_open_file_in)
file = File.new('parm.text', 'w')file.close after finishing with file!File.open which creates a file object if it cannot find one and will close the file
once the code block has ended (so you won't need to close it manually)File.open('parm.text','r') do |fi|
#some code in here
end
w, r, a, w+, r+, a+
r: read from the start, file must already existw: writes from the start, if the file doesn't exist it will create it, if it does
exist, it will wipe the file clean and then apply the text to the beginninga: append to the end of the filer+: read and write to a document, from the start without clearing the page.w+: read and write to a document by clearing the page.a+: read and write to the document, by appending new content to the end.file = File.new(...)
file.puts "abcd" #returns nil
file.print "abcd" #returns nil & no line return
#to use a line return with print:
file.print "hi\n" #use the escape charater
file.write "abcd" #returns the NUMBER of characters added to the file
file << "abcd" #returns the file object
file.close #ALWAYS CLOSE!
gets returns 1 line, more specifically, it returns everything from the current position
to the end of the line!
file.gets #gets 1 line
file.gets.chomp #gets 1 line without the line return
file.read(integer) #returns number of characters
#line return character also counts as a character
#Looping through each line
file.open('filename.txt', 'r') do |file|
while line = file.gets
puts "**" + line.chomp.reverse + "**"
end
end
#OR, ALTERNATIVELY: using method: each_line
file.open("filename.txt", 'r') do |file|
file.each_line {|line| puts line.upcase }
end
file.posfile.read 5
file.pos #returns 5
file.pos = 13
file.eof?, returns true if at end of filefile.pos = 0
#OR
file.rewind
file.pos += 6file.pos -= 2file.pos += 100
file.write "xyz" will insert "xyz" at the new position, but ruby will fill in the
spaces with nil or \000 charactersFile.rename(oldname, newname)File.delete(filename)require 'fileutils'FileUtils.copy("filetobecopied.txt", "copied.txt")File.exist?(file): checks if it existsFile.true?(file): checks to see if the file is indeed a fileFile.directory?(file): checks to see if the file is a directoryFile.readable?(file)File.writable?(file)File.executable?(file)File.size(file): returns number of bytes of the fileFile.dirname(file): returns directory nameFile.ext(file): returns file extensionFile.mtime(file): returns last modified time for the file, 'w'File.atime(file): returns last time the file was accessed, 'r','w'File.ctime(file): returns last time the file's status was changed, NOT WHEN THE FILE WAS
CREATED! 'r','w', changes in permissions, groups or owners, try not to use it!myfile.stat.size or myfile.stat to retrieve a stat object with the stats of the fileDir.entries(arg): arg is the location of where to list the entries for
Dir.foreach(arg): creates the iteration, just pass in the code block codeDir.mkdir(name_of_directory): creates new directoryDir.delete(name_of_directory): deletes the directory (if the directory that contains it
has write permissions AND directory has to be empty!)
Dir.glob('Users/parms/Documents/TVSHOW/Friends/S1*.mkv') will retrieve all files in the path specified
that end with .mkvFileUtils.mv(current_file_path, new_path)