← Back to the catalog

Python for Beginners: Automate CSV Cleanup and File Renaming

Write small Python scripts from zero: read a CSV, clean blanks, stray spaces and mixed casing, count and total a column into a new summary file, and rename a folder of messy files—safely, with a test copy and a dry run first.

Language
English
Lessons
6
Minutes
180

Price: · The subscription includes the whole catalog at no extra cost.

Course outline

  1. Read and clean a CSV with Python

    1. Your first script: lists, loops and if statements (30′)
    2. Read your own CSV and find what is dirty (30′)
    3. Clean the values with plain Python (30′)
  2. Summarize, write out and automate file chores

    1. Summarize the data and write a new CSV (30′)
    2. Rename and sort files in a folder (30′)
    3. Run it safely: test on a copy first (30′)

Free first lesson

Your first script: lists, loops and if statements

A script you can run again

You want to stop renaming files and cleaning spreadsheets by hand. That starts with a script: a plain text file ending in .py, full of instructions Python runs from top to bottom. In your editor (IDLE, VS Code, or a free online editor — all work), type:

``python name = "Report final FINAL v2.pdf" print(name) ``

Save it as first_script.py and run it: the Run button in your editor, or python first_script.py in a terminal opened in the folder where you saved it (python3 on Mac or Linux; on Windows, py first_script.py works if python is not found). The output area shows the file name.

Three words matter here. name is a variable: a label you give a value. "Report final FINAL v2.pdf" is a string: text between quotes. print() shows a value as output. Change the text between quotes, run again, and the output changes — same instructions, new value.

Why save instructions in a file instead of typing them into Python one at a time? Because a file can be run again tomorrow, on a different list of files. That reuse is the whole trick behind automation.

One list, one loop, three files

One variable per file does not scale. A list holds several values in order, in square brackets. A for loop repeats the same lines once per item:

```python files = ["Report final FINAL v2.pdf", "notes .txt", "REPORT v3.pdf"]

for name in files: print(name) ```

Read the for line as "for each item in files, call it name." Python takes the first name, runs the indented line, goes back for the next, and stops when the list ends. The four spaces of indentation say which lines belong to the loop. The output:

``python Report final FINAL v2.pdf notes .txt REPORT v3.pdf ``

Add a fourth name to the list and the output gains a fourth line — nothing else changes. Why does the indented line run three times when you wrote it once? Because the list drives the count: the loop body runs once per item — three names here, three hundred in a real folder.

Pick out only the PDFs

Filtering is where the chore gets real. An if statement runs its indented lines only when a condition is true:

```python files = ["Report final FINAL v2.pdf", "notes .txt", "REPORT v3.pdf"]

count = 0 for name in files: if name.endswith(".pdf"): print(f"PDF: {name}") count = count + 1 print(f"Found {count} PDF files") ```

count = 0 starts a score at zero; inside the if, it grows by one on each match. name.endswith(".pdf") is true only for names ending exactly that way — case matters, so ".PDF" would not match. The f"PDF: {name}" is an f-string: put an f before the quotes and anything in braces is replaced by its value. The last print is not indented, so it runs once, after the loop:

``python PDF: Report final FINAL v2.pdf PDF: REPORT v3.pdf Found 2 PDF files ``

Need a second branch? elif ("else if") is checked only when the if above it was false:

``python if name.endswith(".pdf"): print(f"PDF: {name}") elif name.endswith(".txt"): print(f"Text: {name}") ``

With no further branch, a name matching neither condition is simply skipped — the loop moves on to the next name.

Why does the last print run only once, whatever the list's length? Indentation decides: not indented, not in the loop.

Your turn: only the data files

Here is a folder of monthly reports. The loop is written; the condition is missing:

```python reports = ["jan_sales.csv", "february notes.txt", "Mar_Sales.csv", "april sales.csv"]

for name in reports: if ______________________: print(f"Data file: {name}") ```

Complete the blank so only the files ending in .csv are printed. Before running, write down which names you expect to see; then type the whole script, run it, and check the output against your prediction before comparing with the model answer.

What breaks, and why

  • Every name prints and the if seems ignored: the print must be indented one level deeper than the if (eight spaces inside the loop), or it runs for every item.
  • SyntaxError: expected ':' pointing at a for or if line: that line lost its colon.
  • No matches though the files exist: endswith is case-sensitive — ".PDF" is not ".pdf".

Keep these three

  • A script is a .py file Python runs top to bottom; run it again any time.
  • A for loop runs its indented lines once per item.
  • if and elif run only when the condition holds; endswith is case-sensitive.

On your own files

Pick a folder on your laptop, type five of its real file names into a list, and write a loop with an if that prints only one kind of file (the .jpg, say), counting the matches. Print the count after the loop with an f-string. Run it, then say in your own words what each line does.

script
A plain text file ending in .py that Python runs top to bottom.
variable
A name you give a value so you can use it later, like name or count.
string
Text between quotes, like "notes .txt".
list
Several values in order, written in square brackets.
for loop
Repeats its indented lines once per item of a list.
indentation
The spaces at the start of a line; they decide which lines belong to a loop or an if.
if statement
Runs its indented lines only when its condition is true.
elif
A second condition, checked only when the if above it was false.
f-string
A string with an f before the quotes; anything in braces is replaced by its value.
output
What print shows when the script runs.