Fundamentals 10 min read

Master filestools: Tree View, Diff, Watermark, and Curl-to-Requests in Python

This article introduces the Python filestools library, demonstrating how to install it and use its four core utilities—tree‑style directory listing, text file diff comparison, image watermarking, and converting cURL commands to requests code—complete with command‑line and Jupyter examples.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master filestools: Tree View, Diff, Watermark, and Curl-to-Requests in Python

In this tutorial, the author presents the filestools Python library, which provides four useful utilities for file handling and web request conversion.

Installation

Install the library with a single command:

pip install filestools --index-url https://pypi.org/simple/ -U

1. Tree‑style Directory Display

The tree_dir function recursively shows all files and folders in a specified directory, including their sizes. Example usage in a Windows CMD environment:

# Switch to D: drive
C:\Users\Administrator> D:
# Change to target directory
C:\Users\Administrator> cd C:\Users\Administrator\Desktop\python三剑客\加盟店爬虫

In Jupyter Notebook:

from treedir.tree import tree_dir
tree_dir(r"C:\Users\Administrator\Desktop\python三剑客\加盟店爬虫", m_level=7, no_calc=False)

The function accepts three parameters: path (directory path), m_level (maximum recursion depth, default 7), and no_calc (skip size calculation for deeper folders).

2. Text File Difference Comparison

The file_diff_compare function compares two text files and generates an HTML report highlighting changes (yellow for modifications, green for additions, red for deletions). Example:

from filediff.diff import file_diff_compare
file_diff_compare("a.txt", "b.txt")

The function supports seven optional arguments, such as diff_out (output HTML file name), max_width (line wrap width), numlines (context lines), show_all, and no_browser.

3. Image Watermarking

Use the add_mark function to add a textual watermark to an image. Example:

from watermarker.marker import add_mark
add_mark(file=r"C:\Users\Administrator\Desktop\大学.jpg",
         out=r"C:\Users\Administrator\Desktop\python三剑客\加盟店爬虫",
         mark="黄同学",
         opacity=0.2,
         angle=30,
         space=30)

Key parameters include file, mark, out, color, size, opacity, space, and angle.

4. Convert cURL to requests Code

The library can transform a cURL command into equivalent Python requests code using curl2py:

from curl2py.curlParseTool import curlCmdGenPyScript
curl_cmd = """curl 'http://www.shixi.com/search/index?key=python' \
  -H 'Connection: keep-alive' \
  -H 'Cache-Control: max-age=0' \
  ... (other headers) ...
"""
output = curlCmdGenPyScript(curl_cmd)
print(output)

The generated script can be copied directly into a Python project, simplifying request handling.

All four features have been tested and work as described.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonWatermarkcURLrequestsfile managementdifffilestools
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.