Working with files is a fundamental skill in any programming language. Whether you need to persist user data between program runs, process configuration files, generate reports, or read structured data from spreadsheets, Python’s built-in file I/O tools make the job straightforward. Python provides theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/marioaje/Python/llms.txt
Use this file to discover all available pages before exploring further.
open() built-in for basic file operations and the os module for file-system-level tasks like deleting files and managing directories — all without installing any third-party packages.
Opening and Reading Files
The simplest way to open a file is to callopen() with the filename as its only argument. By default this opens the file in read mode ("r"). Calling .read() on the returned file object loads the entire contents as a string; passing an integer argument reads exactly that many characters.
semana4/archivos/abrirarchivo.py
.readline(), or iterate over every line with a for loop — the most memory-efficient approach for large files.
test/archivo.py
File Modes
open() accepts an optional second argument — the mode — that controls what you can do with the file.
| Mode | Name | Behaviour |
|---|---|---|
"r" | Read | Opens an existing file for reading. Raises FileNotFoundError if the file does not exist. This is the default. |
"a" | Append | Opens a file and writes at the end. Creates the file if it does not exist. |
"w" | Write | Opens a file and writes from the beginning, overwriting all existing content. Creates the file if it does not exist. |
"x" | Create | Creates a new file. Raises FileExistsError if the file already exists. |
Writing and Appending
Use the"a" mode to safely add content to the end of a file (creating it if necessary) and the "w" mode when you want to replace the file’s contents entirely. Always call .close() when you are done, or use with open(...).
semana4/archivos/archivocreador.py
- Append (
"a") tofileOpen.txt— adds the string to whatever is already there. - Append (
"a") toarchivocrear.sql— Python can write any extension, not just.txt. - Write (
"w") todemo.txt— wipes the previous content and replaces it entirely.
Reading CSV Files
Comma-separated values (CSV) files are one of the most common formats for exchanging tabular data. Python’s standardcsv module provides a csv.reader that automatically parses each row into a Python list, handling quoted fields and delimiters correctly.
Combine csv.reader with the with open() context manager so the file is closed automatically once all rows have been processed.
semana4/archivos/leer.py
The
newline='' argument in open() is recommended by Python’s documentation when working with the csv module. It ensures that universal newline translation is disabled so that the CSV reader can handle line endings inside quoted fields correctly.Deleting Files
Use theos module to remove files from the file system. Always check that the file exists first with os.path.exists() to avoid an FileNotFoundError.
semana4/archivos/borrararchivo.py
os.path.exists(variable)returnsTrueif the path points to an existing file (or directory).os.remove(variable)deletes the file permanently — there is no recycle bin.- The
elsebranch gives the user a clear message when the file is not found.
Creating and Removing Directories
Theos module also lets you create and remove directories. os.mkdir() creates a single directory at the given path. Combine it with os.path.join() to build paths in a cross-platform way instead of hard-coding slashes.
test/folder.py
Removing a directory with os.rmdir()
Removing a directory with os.rmdir()
To remove an empty directory use The commented-out line
os.rmdir("folder_name"). The directory must be empty; if it contains files or subdirectories, use shutil.rmtree() from the shutil standard-library module instead.semana4/archivos/borrandocarpeta.py
os.rmdir("test") shows the removal syntax. The active line demonstrates that os.mkdir() can also be called without a joined path when the target directory sits in the current working directory.