Open In App

Working with Text Files in Julia

Julia is Programming Language that is fast and dynamic in nature (most suitable for performing numerical and scientific applications) and it is optionally typed (the rich language of descriptive type and type declarations which is used to solidify our program) and general-purpose and open-sourced. Julia supports file handling like other programming languages with much higher efficiency and associated methods i.e reading, writing, and closing a file. It is a combination of  C language ( powerful performance) and Python( simplicity).

Creation of a File

In order to work with a file in Julia, firstly we need to create a new file using “touch”(used to create a new empty file) method, “pwd”(used for checking the present working directory of the system) method and “cd”(used to change directory and create the file where we want) method.



touch("example.txt")

File Created 

Opening an existing File

To open the file in Julia we have “open” method. The open method takes in two arguments i.e filename (file to open) and mode of operation (read, write or append).

For read mode we use (“r”), write mode(“w”), and append mode(“a”).






# here we open file in read mode 
abc = open("example.txt","r"
  
# here we open file in write mode 
efg =  open("example.txt","w"
  
# here we open file in append mode
hij =  open("example.txt","a")

file open in different mode

Just like Python, Julia also has a “do method that helps us to avoid the mode of operation and prevent us from the issue of closing the file every time after modifying it.




open("example.txt") do file
    # modify mile content 
end

Reading Contents of a File

Reading entire file

Here we will use “read” method to read the content of a file and it takes in two arguments i.e file to read and method to read the file as (string, integer, etc).




# METHOD 1
# reading the content of file as a string
mydata = read(abc, String)  
  
# METHOD 2
mydata = open("example.txt") do file
    read(file, String)
end

File Read 

Reading file line by line

Here we  will use “readlines” method to ready the file as an array with each line of a file as an array element.




# open file 
mni = open("example.txt","r");
  
# reading file content line by line
line_by_line = readlines(mni)

Writing to a File

Here we will change the mode of the file from read to write in order to write file inside “open” method.

 Using write mode 




# open file in write mode
abc = open("example.txt", "w")

Now , we will use write method in Julia to write content to the “example.txt ” file, and write method will delete the previous content from file or will create the file if it does not exist.




# writing to a file using write() method 
write(abc, " Hello World , Julia welcomes you"
    
# We need to close the file in order 
# to write the content from the disk to file 
close(abc)

Using append mode




# open file in append mode
abc = open("example.txt", "a"
  
# writing to a file using write() method 
write(abc, " Hello World , Julia welcomes you"
    
# We need to close the file in order to write the content from the disk to file 
close(abc)

Modifying the contents of a file

Here we will learn to modify the content of a file i.e we will change the case of content in a file from uppercase to lowercase using the lowercase method.




open("example.txt") do abc 
  
       # change the case of content 
       # from uppercase to lowercase
       lowercase(read(abc, String)) 
end

Closing the file

Here we will use the close method in Julia to close the file after modifying its content.




close(abc) # close method will close the file


Article Tags :