October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Skip the First Line in a File in Python

Use next(f, None) to skip the first line of a text file safely. For CSV, distinguish discarding the first record from using it as a header.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a regular text file, consume one line before starting the loop. Use next(f, None) to make an empty file safe:

with open("input.txt", encoding="utf-8") as f:
    next(f, None)
    for line in f:
        process(line)

If the file is CSV, skip a parsed CSV record with csv.reader or let csv.DictReader use the first row as column names. Those cases require different code.

Skip one line in an ordinary text file

A Python file object can be iterated line by line. Calling next(f, None) advances it once, so the following loop starts with the second line. The default None means an empty file does not raise an exception.

with open("input.txt", encoding="utf-8") as f:
    next(f, None)  # discard the first line, if present
    for line in f:
        process(line)

Python documents file iteration as a memory-efficient way to read lines without loading the whole file into memory: Input and Output: Reading and Writing Files.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use readline() instead

You can also read and discard the first line explicitly. At end-of-file, readline() returns an empty string, so this approach is also safe on an empty file:

with open("input.txt", encoding="utf-8") as f:
    f.readline()
    for line in f:
        process(line)

A blank first line is not the same as an empty file: the blank line contains a newline character, while an empty string from readline() indicates end-of-file. If you use next(f) without a default, it raises StopIteration when the file has no lines.

Skip the first CSV record

Use Python’s csv module when the input is CSV. A record can contain embedded newlines, so one physical line of text is not necessarily one CSV record. The reader parses records and yields each as a list:

import csv

with open("input.csv", newline="", encoding="utf-8") as f:
    reader = csv.reader(f)
    next(reader, None)  # discard the first parsed record, if present
    for row in reader:
        process(row)

Open the file with newline="" when passing it to csv.reader, as specified in the Python CSV documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use the first CSV row as column names

If the first CSV record contains headers such as name and email, csv.DictReader uses that row as field names by default. It omits the header from the records it returns:

import csv

with open("input.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        process(row["name"])

Do not call next() on the file before creating this DictReader: doing so consumes the header, causing the first data record to be used as field names instead. If the CSV has no header, supply fieldnames deliberately; then the first row is treated as data.

Choose the right operation

  • Plain text, discard one initial line: call next(f, None) or f.readline() before iterating.
  • CSV, discard one record: call next(reader, None) on a csv.reader.
  • CSV with a header: iterate over csv.DictReader(f) without consuming a row first.
  • CSV without a header: define field names if using DictReader, so its first data record is not mistaken for a header.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.