$ ls ~yifei/notes/

Python `-m`

Posted on:

Last modified:

How -m is invoked

If you have a module foo

foo/
    __init__.py
    __main__.py

Then python -m foo will execute __main__.py

json pretty print

cat some_json_file | python -m json.tool

zipfile

zipfile.py -l zipfile.zip        # Show listing of a zipfile
zipfile.py -t zipfile.zip        # Test if a zipfile is valid
zipfile.py -e zipfile.zip target # Extract zipfile into target dir
zipfile.py -c zipfile.zip src ... # Create zipfile from sources

gzip

python -m gzip wordlist.txt  # Will create wordlist.txt.gz
python -m gzip -d wordlist.txt.gz  # Will extract to wordlist.txt

filecmp

compare two directories

$ python -m filecmp /tmp/a /tmp/b
diff /tmp/a /tmp/b
Only in /tmp/a : ['1']
Only in /tmp/b : ['2']
Identical files : ['4']
Differing files : ['3']

Several modules lets you encode/decode in various formats:

  • base64
  • uu
  • encodings.rot_13
  • binhex
  • mimify
  • quopri

For example:

$ echo 'secertpassword' | python -m encodings.rot_13
frpregcnffjbeq

http servers

python -m SimpleHTTPServer # 2
python -m http.server #3
python -m pydoc {module/func}

Profiling

python -m cProfile script.py
python -m timeit script.py
python -m pstats script.py
python -m trace script.py

References

https://pythonwise.blogspot.sg/2015/01/python-m.html

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.