TutorialsPythonFile Handling in Python

File Handling in Python

Read and write CSV, Excel, and text files — the entry point of every data project

Almost every data analysis starts with reading a file. Python can read and write CSV files, Excel workbooks, JSON, text files, and more. With pandas, loading a CSV is a single line. Knowing how to handle files well — specifying encodings, skipping bad rows, reading specific sheets — separates analysts who write robust scripts from those whose code breaks on every new dataset.

Examples

Reading files with pandas
import pandas as pd

# CSV — the most common
df = pd.read_csv("sales.csv")

# CSV with options (very common in real data)
df = pd.read_csv(
    "sales.csv",
    encoding="utf-8",        # or "latin-1" for Indian data
    sep=",",                 # or "\t" for tab-separated
    skiprows=2,              # skip first 2 rows
    nrows=1000,              # load only first 1000 rows
    parse_dates=["OrderDate"],  # auto-parse date columns
    na_values=["N/A", "-", "NULL"]  # treat as NaN
)

# Excel
df = pd.read_excel("report.xlsx", sheet_name="Sales")
df = pd.read_excel("report.xlsx", sheet_name=0)  # first sheet

# All sheets at once (returns dict)
all_sheets = pd.read_excel("report.xlsx", sheet_name=None)
# all_sheets["Sheet1"] gives you that sheet's DataFrame
Writing output files
# Save to CSV (no index column)
df.to_csv("output.csv", index=False)
df.to_csv("output.csv", index=False, encoding="utf-8-sig")  # Excel-compatible UTF-8

# Save to Excel
df.to_excel("output.xlsx", sheet_name="Cleaned Data", index=False)

# Save multiple sheets to one Excel file
with pd.ExcelWriter("multi_sheet_report.xlsx") as writer:
    df_sales.to_excel(writer, sheet_name="Sales", index=False)
    df_summary.to_excel(writer, sheet_name="Summary", index=False)
    df_regional.to_excel(writer, sheet_name="Regional", index=False)

# Read raw text file (logs, notes)
with open("notes.txt", "r", encoding="utf-8") as f:
    content = f.read()
    lines = content.splitlines()
💡 Always use index=False when saving CSVs — otherwise pandas adds an unwanted row number column.

Key Points

  • pd.read_csv() and pd.read_excel() are the two most-used file loading functions
  • encoding="latin-1" fixes UnicodeDecodeError on files with Indian language characters
  • Always use index=False in to_csv() and to_excel() to avoid the extra index column
  • parse_dates=["col"] auto-converts date strings to datetime objects
  • ExcelWriter context manager saves multiple DataFrames to multiple sheets in one file

Practice Question

You saved a DataFrame with df.to_csv("output.csv") and notice an extra unnamed column with numbers 0,1,2... when opening in Excel. What caused this?