Python I/O made easy with filehandlers
Tags: python, io, files
Published: 2019-09-27 15:15:29

Hello everybody,
Today I am here to explain how my library, filehandlers, makes basic Python I/O easier for everyone.

Let me start off by saying that Python's `open` built-in function can be confusing. While it is well documented on 3rd party websites, integration in general can be difficult. The result of using the function is a `io.TextIOWrapper` object, which by itself does have documentation, but still can be hard for beginners and advanced Python developers alike.

(Before you can continue, filehandlers needs to be installed. You will need to install the `filehandlers` package with pip ([guide](https://packaging.python.org/tutorials/installing-packages/)), or add it to your `requirements.txt`.)

## Creating Files

Let's start off with the bare minimum, creating a file in the working directory.

```python
# import the library
# make sure you followed the steps in the parentheses above!
import filehandlers

# define a "file", even if said "file" doesn't yet exist
# this call just creates an instance, nothing too spooky going on here
myfile = filehandlers.AbstractFile("mycoolfile.txt")

# create the file on the disk
myfile.touch()

# note in the console what great work we have done
print("I created the file!")
```

Easy, right?

## Writing to Files

Now, let's create a file, and write a hello world to it:

```python
# same basic starter steps
import filehandlers

myfile = filehandlers.AbstractFile("mycoolfile.txt")

# create the file on the disk
myfile.touch()

print("I created the file!")

# create a manipulator object so we can change the file:
manipulator_of_myfile = filehandlers.FileManipulator(
    # we need to input the AbstractFile instance to this function:
    myfile
)

# now, we will write "hello world!" to the file:
manipulator_of_myfile.write_to_file("hello world!")

# and we are done
print("Mission complete.")
```

Now that we have created files *and* written to files, I think it's time we read files, so we can use existing data.

## Reading Files

Alright, let's just get right into this:

```python
# this time, we'll just import the modules we need
# so there is no need to fully type out the class names inline
from filehandlers import AbstractFile, FileManipulator

myfile = AbstractFile("data.cfg")

manipulator = FileManipulator(myfile)

# we just created the manipulator, so the file cache should be up to date
# if at any time you believe the file may have changed, you can run:
# my_manipulator_variable.refresh()

# get the file's line cache
# (this will be an array, each index will be the next line, so the_cache_element[0] will be the first line, the_cache_element[1] will be the second line, etc. etc.)

the_cache_element = manipulator.get_cache()

# print the first line of the file
print("Line 1: " + the_cache_element[0])

# print all of the lines:
for line in the_cache_element:
    print(line)
```

Super simple.

If you would like to do further reading, full docs can be found [here](https://filehandlers.rdil.rocks/en/stable).

Want to contribute? The library is open source and can be found [here](https://github.com/RDIL/filehandlers).