Skip to content
Related Articles

Related Articles

Opening and Reading a File in Julia

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 20 Jul, 2021

File handling in Julia is achieved using functions such as open(), read(), close(). There are many ways to read the contents of a file like readline(), readlines() and just read().

  • open(): To open a file existing in an absolute path, provided as the parameter. 
  • read(): Read the contents of the file into a single string. 
  • close(): Close the file object or the variable holding the instance of an opened file. 

Opening a File

Consider a file “text.txt” placed in the same working directory along with Julia’s implementation file.


Now we will use open() function to open a file in readable mode 
Syntax : 

f = open("absolute path of the file", "r")

# few file operations 

There are two methods to Open a file using open() function: 

Method 1

Open a file using open() and assign it to a variable which is the instance of the opened file, then make use of that variable for further operations to be performed on the opened file.Then close the file using close().


# Opening a file in read_mode, method 1
# r is the default mode
f = open("test.txt", "r")
# do some file operations
# close the file instance

Method 2

Open a file in union with a ‘do’ control flow. The opened file will automatically be closed once the end of do loop is attained. Perform the desired operations within the do control flow. This is the most common and handy way of opening and accessing a file.


# opening a file in read_mode, method 2
open("test.txt") do f
    # do stuff with the open file instance 'f'
# opened file is automatically closed after the do control ends.

Reading the contents of a file

Reading a File can be done in multiple ways with the use of pre-defined functions in Julia. Files can be read line by line, in the form of strings or the whole file at once.

Read the file contents line by line using readline() Function

Suppose a file has n lines within. We can use the readline() function to read the contents of the file in a line by line manner(one line at a time) until the EOF (End of file) is attained.


# read file contents, one line at a time
open("test.txt") do f
  # line_number
  line = 0  
  # read till end of file
  while ! eof(f) 
     # read a new / next line for every iteration          
     s = readline(f)         
     line += 1
     println("$line . $s")



Reading the lines of a file into a String array using readlines()

readlines() method is used to read all the lines of the file into a String array. Where every element is a line of the file. Using this we can read multiple lines at once.


# opening a file in read_mode
# r is the default mode
f = open("test.txt", "r")
# to count total lines in the file
line_count = 0             
for lines in readlines(f)
    # increment line_count
    global line_count += 1 
    # print the line
# total lines in file
println("line count is $line_count")



Read all contents of a file into a String at once using read()

The file contents are entirely read into a single string using the read() method.

s = read(f, String)

# f can be a file object or an absolute path



# opening a file in read_mode
f = open("test.txt", "r")
# read entire file into a string
s = read(f, String)      



File Exception handling

Till now we have assumed the case where our file exists in the specified path. What if a file doesn’t exist in the specified path?


# opening a file in read_mode
# dummy.txt doesn't exist
# in the working directory
f = open("dummy.txt", "r")        




We will handle this using the Try-Catch block in Julia.Open the file in try block using open(), if the file exists, then perform the desired operation. Else the try block will throw an exception and the control reaches the catch block. The user can be warned in the try block using a println() message or @warn.


# since the presence of file is uncertain we'll use try catch
    open("dummy.txt", "r") do s
        # perform desired operations if file exists
    # either warn or print that the file doesn't exist
    println("file doesn't exist")



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!