DEV Community

Reece Dunham
Reece Dunham

Posted on • Edited on

Python I/O made easy with filehandlers

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), or add it to your requirements.txt.)

Creating Files

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

# 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!")
Enter fullscreen mode Exit fullscreen mode

Easy, right?

Writing to Files

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

# 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.")
Enter fullscreen mode Exit fullscreen mode

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:

# 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)
Enter fullscreen mode Exit fullscreen mode

Super simple.

If you would like to do further reading, full docs can be found here.

Want to contribute? The library is open source and can be found here.

Top comments (2)

Collapse
 
rhymes profile image
rhymes

Hi Reece!

There's obviously nothing wrong in reinventing the wheel but since I didn't see it mentioned I suspected you were not aware about Python's pathlib module in the standard library.

It has concepts of abstract and concrete paths. You can create files, directories, read and write both binary and text content.

It was created exactly for the reason you mention, how cumbersome the low level stuff was.

Take a look, maybe it serves all your needs or maybe you can just wrap yours on top of it for what's missing

Collapse
 
rdil profile image
Reece Dunham

Very interesting! I indeed didn't know about this, and will try to come up with some kind of wrapper for it. Thanks for pointing it out :)