Ruby Way IO II

Source: Internet
Author: User

1 detect whether the file exists and its size

Filetest's exist? Method can detect whether a file exists:

Flag = filetest::exist? ("Lochnessmonster")
Flag = filetest::exists? ("UFO")
# exists? is a synonym for exist?

If we want to know if the file has content, we can use File::stat zero? Method:

Flag = file.new ("Somefile"). Stat.zero?

This will return true, because 0 is also true,nil false in Ruby.

So we can use size? Method:

If File.new ("MyFile"). Stat.size?
puts "The file has contents."
Else
puts "the file is empty."
End

Filetest module inside also have zero? and size? Method:

Flag1 = Filetest::zero? ("File1")
Flag2 = filetest::size? ("File2")

Here's another size method:

Size1 = File.size ("file1")
size2 = File.stat ("File2"). Size

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.