Skip to content

Easy File Manager

py_simple.easy_file_manager

easy_file_manager is meant to simplify working with files.

InvalidExtension

Bases: Exception

Exception raised for invalid extension

add_a_line(filename, line)

Add line to existing file. If file does not exist, create it.

Parameters:

Name Type Description Default
filename str

File to write to.

required
line str

Line to write.

required
Example
from py_simple import add_a_line

add_a_line("new_file.txt", "hello world!")
with open("new_file.txt", "a", encoding="utf-8") as f:
    f.write("hello world!" + "\n")

copy_file(source, destination)

Copy a file to a new location or name.

Parameters:

Name Type Description Default
source str

File to copy.

required
destination str

Destination path or filename.

required
Example
from py_simple import copy_file

copy_file("notes.txt", "notes_backup.txt")
import os
import shutil

source, destination = "notes.txt", "notes_backup.txt"
if os.path.isfile(source) and not os.path.isfile(destination):
    shutil.copy2(source, destination)

is_file_there(filename)

Check if file exists in current working directory.

Parameters:

Name Type Description Default
filename str

Name of the file to be checked.

required

Returns:

Name Type Description
bool bool

True if file exists, False otherwise.

Example
from py_simple import is_file_there

exists = is_file_there("new_file.txt")
import os

exists = os.path.isfile("new_file.txt")

list_files(extension=None)

List files in the current working directory that have valid extensions. Optionally filter by a specific extension.

Parameters:

Name Type Description Default
extension str

Filter by extension (e.g., 'txt'). If None, all valid extension files are listed.

None

Returns:

Name Type Description
list list

Sorted list of filenames matching the filter.

Example
from py_simple import list_files

all_valid = list_files()
txt_only = list_files("txt")
import os

valid_extensions = ["txt", "md", "log", "csv"]
matches = [
    f for f in os.listdir(".")
    if os.path.isfile(f) and f.split(".")[-1].lower() in valid_extensions
]
matches.sort()

make_blank_file(filename, file_extension)

Creates a blank file in current working directory. If file already exists, nothing is done.

Parameters:

Name Type Description Default
filename str

The name of the file to be created.

required
file_extension str

The extension without '.'. Allowed: ['txt', 'csv', 'md', 'log']

required
Example
from py_simple import make_blank_file

make_blank_file("new_file", "txt")
import os

filename = "new_file.txt"
if not os.path.isfile(filename):
    with open(filename, "w", encoding="utf-8"):
        pass

read_file_to_list(filename)

Reads lines of existing file to list.

Parameters:

Name Type Description Default
filename str

File to read from.

required

Returns:

Name Type Description
list list

List of lines.

Example
from py_simple import read_file_to_list

lines = read_file_to_list("new_file.txt")
try:
    with open("new_file.txt", "r", encoding="utf-8") as f:
        lines = [line.strip() for line in f.readlines()]
except FileNotFoundError:
    lines = []

remove_file(filename)

Removes file from current working directory.

Parameters:

Name Type Description Default
filename str

File to delete.

required
Example
from py_simple import remove_file

remove_file("new_file.txt")
import os

filename = "new_file.txt"
if os.path.isfile(filename):
    os.remove(filename)

rename_file(old_name, new_name)

Rename file in the current working directory.

Parameters:

Name Type Description Default
old_name str

Current filename.

required
new_name str

New filename.

required
Example
from py_simple import rename_file

rename_file("old_name.txt", "new_name.txt")
import os

old_name, new_name = "old_name.txt", "new_name.txt"
if os.path.isfile(old_name) and not os.path.isfile(new_name):
    os.rename(old_name, new_name)