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.
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/ -U1. 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
