JES is a programming environment that we can use to test, write, and run code locally on our computer. However, for this you also need to know How to read and write files using the JES applicationwhich even has several functions for the possibility of editing photos and videos, among different media.
Different types of files can be read, including text and CSV files. So it is considerably useful on various occasions.
How to read and write files with JES
read a file
To be able to read a file in JES, you will first need to open it, then view the content and save it as a variant for later processing.
In case of reading a text file, we can save each line of the file individually as an element in an array. In the case of CSV files, it is possible to store the value of each individual cell.
Therefore, we start by opening the JES application on our computer. In the programming window we are going to have to create a new function:
We’ll use the pickAFile() function to prompt you to select a file:
file = pickAFile()
Now file validation to make sure users only select TXT or CSV files:
if not file.endswith(«.txt») and not file.endswith(«.csv»): print(«Error: Only .txt and .csv files are supported.») return
We will open the file we chose with the open() function. As for the first parameter, it is the file we are reading. Regarding the second, it is in charge of defining the mode we use to open it. So rt means read file.
openedFile = open(file, “rt”)
After opening the file, we are going to have to read all the contents inside. We proceed to save this content of the file in a variable:
allContent = openedFile.read()
Now, we close the file:
The variable “allContent” has a single string that is responsible for saving all the content of the file. A newline character (n) is the one that breaks each line into files. If we want to access individual lines, we’ll split a string and save each line as an element in an array:
if file.endswith(«.txt»): rows = allContent.split(«n») print(rows)
If we are reading a CSV file, we can separate the lines to get the value of each individual cell. For each row in the CSV file, we will need to separate the values using commas and store the values in a double array. The structure of the array should look something like this: [[1,2,3], [4,5,6], [7,8,9]].
if file.endswith(«.csv»): rows = allContent.split(«n») csvData =for row in rows: if (row != »): cells = row.split(«,») csvData.append([float(cell) for cell in cells]) print(csvData)
Now, we would have to test this program, for this we are going to create a text file called sample.txt and fill it with some text, it can be anything.
As an alternative, we can create a CSV file, the name will be numbers.csv and we will fill it with some data.
In the JES application, we will click on the Load program button, it is located between the programming window and the command line.
Here we are going to execute the function that we will see next on the command line:
Using the file prompt, we’re going to navigate to where we’ve saved the sample.txt file. We will choose the file to be able to open it and see what will appear on the screen.
We run the redFromFile() function again from the command prompt. We choose the numbers.csv file to see what appears in the console, with each cell individually and stored in an array.
How to write a file in JES
Thanks to the write() function we can write a text or CSV file. It is possible to open the file by adding or saving it. The additional data will be added to the content that already exists, while the write will overwrite any existing content in the file.
So we start by creating a new function and we’ll use it to write the text and CSV file. We start by creating the function called writeToFile():
Next, we are going to use the pickAFile() function to select a file:
file = pickAFile()
We open the file to add all the data:
openedFile = open(file, “at”)
As an alternative, if what we want is to overwrite the entire content of the file, we will have to enter “w” as an argument instead of “at”.
openedFile = open(file, “w”)
If what we are looking for is to write several files. We can add different lines, we will use “n” to divide the content into lines or the write() function:
if file.endswith(«.txt»): openedFile.write(«nTesting») openedFile.write(«nTesting1nTesting2») openedFile.write(«nTesting3»)
To write a CSV file, we will have to write all the data in a row using the write() function and we will separate the values of each cell using a comma:
if file.endswith(“.csv”): openedFile.write(“n12,34,56”)
Once we finish, we proceed to close the file:
openedFile.close() print(“Wrote to file successfully”)
Now we are going to click on the Load program button, this is located between the programming and command line options. Here we will execute the following command in the command prompt:
Using the file indicator, we will select the file “sample.txt”. After JES finishes writing the file, we proceed to open it to see the lines that were added to the end of it.
We’ll run the writeToFile() function again from the command line. We open the numbers.csv file so we can see the values that were added to the cell, right at the bottom.
As we can see, it is important to learn to read and write files using JES is something extremely useful that, in principle, can be somewhat complicated. However, it ends up being very comfortable to save data within a program.