# Creating Files

**URL:** <https://www.ruby-forum.com/t/creating-files/72671>\
**Category:** Ruby\
**Created:** [February 24, 2006, 3:55am UTC](https://www.ruby-forum.com/t/creating-files/72671 "2006-02-24T03:55:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Ferra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@Andy\_Ferra](https://www.ruby-forum.com/u/Andy_Ferra)\
**Post date:** [February 24, 2006, 3:55am UTC](https://www.ruby-forum.com/t/creating-files/72671/1 "2006-02-24T03:55:42Z")

</div>

Could someone please tell me how to create a new file?

Thank you.

---

<div class="post-metadata">

**Author:** ![Andy\_Ferra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@Andy\_Ferra](https://www.ruby-forum.com/u/Andy_Ferra)\
**Post date:** [February 24, 2006, 4:24am UTC](https://www.ruby-forum.com/t/creating-files/72671/2 "2006-02-24T04:24:16Z")

</div>

Andy Ferra wrote:

> Could someone please tell me how to create a new file?

You could use FileUtils.touch or just File.open a filename  
in write-mode. You can take a look at the documentation for  
those classes by typing

ri FileUtils  
ri File

On your command-line.

> Thank you.

E

---

<div class="post-metadata">

**Author:** ![Andy\_Ferra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@Andy\_Ferra](https://www.ruby-forum.com/u/Andy_Ferra)\
**Post date:** [February 24, 2006, 6:00am UTC](https://www.ruby-forum.com/t/creating-files/72671/3 "2006-02-24T06:00:34Z")

</div>

Hi,

For an empty file, I guess the easiest way would be:

# Create a new file and write on it

# 

begin  
f=File::open(“test.txt”,“w”)  
rescue SystemCallError  
puts “Problem with File::open #1”  
#… something went wrong…  
else  
f.close  
end

# The file will be closed afterwards

# 

begin  
File::open("/etc/test.txt",“w”) do | f |  
# Do somethinf with “f” …  
end  
rescue SystemCallError  
# The file can’t be created  
puts “Problem with File::open #2”  
end

# If all you want is create an empty file…

# 

begin  
File::open(“test.txt”,“w”) {}  
rescue SystemCallError  
# The file can’t be created  
puts “Problem with File::open #3”  
end

read “ri IO::open” for more info!

Merc.
