Create files with 10 extensions

EasyOS Module Programs · 34 of 49

Write a Python program to create sample files with 10 different extensions.

Advertisement
Output
10 ['sample.csv', 'sample.html', 'sample.ini', 'sample.json']

Approach

  1. Loop over a list of extensions.

Solution

Python
import os os.makedirs("fake", exist_ok=True)extensions = ["txt", "csv", "json", "py", "md", "log", "xml", "html", "yaml", "ini"]for ext in extensions:    with open(os.path.join("fake", f"sample.{ext}"), "w") as f:        f.write(f"sample {ext} data\n")print(len(os.listdir("fake")), sorted(os.listdir("fake"))[:4])
Output
10 ['sample.csv', 'sample.html', 'sample.ini', 'sample.json']

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Leave a Comment