work with files in python


May security files are logs. To get through the massive amount of data collected in logs, you’ll need python to parse this information.

#open a text file
with open("login_attempts.txt", "r") as file:
    file_text = file.read()
print(file_text)
  • “w” – write
  • “r” – read
  • “a” – append

You can also use the open() function without the with keyword. However, you should close the file you opened to ensure proper handling of the file.

If the file is in the same directory as the python code you only need to have the name of the file written in the code. However if the file is not in the same directory you will have to include a file path, eg:

with open("/home/analyst/logs/login_attempts.txt", "r") as file

In python, the names of files or their file paths can be handled as string data.

reading files in python

#open a text file
with open("login_attempts.txt", "r") as file:
    file_text = file.read()
print(file_text)

The above code imports “login_attempts.txt” into the file_text variable.

The .read() method converts files into strings. The file_text variable is used to generate a string of the file contents through .read(). The string is then stored in the file_text variable.

Once it is stored in the variable you can perform the same operations on it that you might perform on any other string.

writing files in python

Sometimes you may need to write to files. Perhaps you need to create a file containing approved usernames on a new allow list.

Writing to a file uses the "w" and "a" argument in open().

Using the "w" argument when you want to replace the contents of an existing file. This argument opens the file so that contents can be replaced. Additionally you can use the "w" argument to create a new file.

Using the "a" if you want to append new information to the end of an existing file rather than writing over it. It opens the file so that new information can be appended to the end. No existing information will be deleted.

parse a text file in python

Parsing is the process of converting data into a more readable format.

Methods that can help parse data are .split() and .join()

.split()

The .split() method converts a string into a list. It separates the string based on a specified character that’s passed into .split() as an argument.

A string containing the comma is passed into .split() in order to parse it into a list.

approved_users = "elarson,bmoreno,tshah,sgilmore,eraab"
print("before .split():", approved_users)
approved_users = approved_users.split(",")
print("after .split():", approved_users)

Returns:

before .split(): elarson,bmoreno,tshah,sgilmore,eraab
after .split(): ['elarson', 'bmoreno', 'tshah', 'sgilmore', 'eraab']

If you do not pass an argument into .split() it will separate the string every time it encounters a whitespace.

removed_users = "wjaffrey jsoto abernard jhill awilliam"
print("before .split():", removed_users)
removed_users = removed_users.split()
print("after .split():", removed_users)

Returns:

before .split(): wjaffrey jsoto abernard jhill awilliam
after .split(): ['wjaffrey', 'jsoto', 'abernard', 'jhill', 'awilliam']

Because an argument isn’t passed into .split() python splits the removed_users string at each space when separating it into a list.

applying .split() to files

The .split() method allows you to work with file content as a list after you’ve converted it to a string through the .read() method. This is useful in a variety of ways. For example, if you want to iterate through the file ocntents in a for loop, this can be easily done when it’s converted into a list.

The following code opens the “update_log.txt” file. Then reads all of the file contents into the updates variable as a string and splits the string in the updates variable into a list by creating a new element at each whitespace:

with open("update_log.txt", "r") as file:
    updates = file.read()
updates = updates.split()

After this, through the updates variable, you can work with the contents of the “update_log.txt” file in parts of your code that require it to be structured as a list.

.join()

If you need to convert a list into a string use .join()

This method concatenates the elements of an iterable into a string. The syntax used with .join() is distinct from the one used with .split() and other methods, like .index()

With .join() you must pass the list that you want to concatenate into a string in as an argument. You append .join() to a character that you want to separate each element with once they are joined into a string.

approved_users = ["elarson", "bmoreno", "tshah", "sgilmore", "eraab"]
print("before .join():", approved_users)
approved_users = ",".join(approved_users)
print("after .join():", approved_users)

Returns:

before .join(): ['elarson', 'bmoreno', 'tshah', 'sgilmore', 'eraab']
after .join(): elarson,bmoreno,tshah,sgilmore,eraab

Before .join() is applied, approved_users is a list of five elements. After it is applied it is a string with each username separated by a comma.

applying .join() to files

When working with files it may be necessary to convert its contents back into a string. You may want to use the .write() method. The .write() method writes string data to a file. This means that if you have converted a file’s contents into a list while working with it, you’ll need to convert it back inot a string before using .write(). You can use the .join() method for this.

If you want to repalce”update_log.txt” with new contents, you need to first convert updates back into a string using .join(). Then you can open the file using a with statement and use the .write() method to write the updates string to the file:

updates = " ".join(updates)
with open("update_log.txt", "w") as file:
    file.write(updates)

The code " ".join(updates) indicates to separate each of the list elements in updates with a space once joined back into a string. And because “w” is specified as the second argument of open() python will overwrite the contents of “update_log.txt” with the string currently in the updates variable.

Leave a Reply

Your email address will not be published. Required fields are marked *