Modules - Python practice book

1/4/2018, 8:16:00 PM

  1. Write a program to list all files in the given directory.
import os
def listdir(dir, handler):
  with os.scandir(dir) as files:
    for file in sorted(
      key=lambda file:file.stat().st_size,
listdir(os.getcwd(), lambda file:print(
  1. Write a program to count number of files for each extension in the given directory. The program should take a directory name as argument and print count and extension for each available file extension.
cache = dict()
def statisticext(file, mem = cache):
  if file.is_file():
    name, ext = os.path.splitext(
    if ext in mem:
      mem[ext] += mem[ext]
      mem[ext] = 1

listdir(os.getcwd(), statisticext)

for ext, count in cache.items():
  print('{count:<4} {ext:<10}'.format(ext=ext, count=count))
  1. Write a program to list all the files in the given directory along with their length and last modification time. The output should contain one line for each file containing filename, length and modification date separated by tabs.
import math

def convert(bytes, lst = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']):
  i = 0 if bytes < 1024 else int(math.floor(math.log(bytes, 1024)))
  l = len(lst)
  if i >= l:
    i = l - 1
  return '{value:.2f} {strlst}'.format(
    value = bytes/math.pow(1024, i),
    strlst = lst[i]

def formatfilelist(file):
  stat = file.stat()
    name =,
    length = convert(stat.st_size),
    modifytime = time.strftime(
      '%Y/%m/%d %H:%M:%S',

listdir(os.getcwd(), formatfilelist)
  1. Write a program to print directory tree. The program should take path of a directory as argument and print all the files in it recursively as a tree.
import time
sep = '/'
def tree(dir, layer = 0, base = ''):
  if base == '':
    base = os.path.basename(dir)
  if layer == 0:
    print(base + sep)
    print('|  ' * (layer - 1) + '|--' + base + sep)
  with os.scandir(dir) as files:
    for file in sorted(files, key=lambda file:file.is_dir(), reverse=True):
      if file.is_dir():
        tree(file.path, layer + 1, base = os.path.relpath(file.path, dir))
        print('|  ' * layer + '|--' +

  1. Write a program to download a given URL. The program should accept a URL as argument, download it and save it with the basename of the URL. If the URL ends with a /, consider the basename as index.html.
import sys
from urllib.request import urlopen

# 依赖第三方模块
import chardet

def wget(url):
  # 简单处理
  filename = 'index.html' if url.endswith('/') else url[url.rfind('/') + 1:]
  with open('./download/' + filename, 'w', encoding='utf-8') as f:
    with urlopen(url) as res:
      data =
      charset = chardet.detect(data)

if (__name__ == '__main__'):
  print('wget:', sys.argv[1])
  1. Write a program that takes a URL as argument, downloads the html from web and print it after stripping html tags.

  2. Write a function make_slug that takes a name converts it into a slug. A slug is a string where spaces and special characters are replaced by a hyphen, typically used to create blog post URL from post title. It should also make sure there are no more than one hyphen in any place and there are no hyphens at the biginning and end of the slug.

import re

plist = [
  'hello world',
  'hello   world',
  '   --- hello-  world---'

def make_slug(string):
  return re.sub(r'[-\s]+', '-', string.strip('- '))

  [make_slug(item) for item in plist]
  1. Write a program that takes URL of a webpage as argument and prints all the URLs linked from that webpage.

  2. Write a regular expression to validate a phone number.


  1. Write a program to print the external IP address of the machine. Use the response from and read the IP address from there. The program should print only the IP address and nothing else.
import json
from urllib.request import urlopen

u = ''
with urlopen(u) as res:
  string ='utf-8')
  data = json.loads(string)
  1. Write a python program to create a zip file. The program should take name of zip file as first argument and files to add as rest of the arguments.

  2. Write a program to implement the functionality of pydoc. The program should take the module name as argument and print documentation for the module and each of the functions defined in that module.

  3. Write a program that reads a csv file and exports it as Excel file. The prigram should take two arguments. The name of the csv file to read as first argument and the name of the Excel file to write as the second argument.

  4. Create a new virtualenv and install BeautifulSoup. BeautifulSoup is very good library for parsing HTML. Try using it to extract all HTML links from a webpage.

Python TODO