The gMan nixWiki

Because the mind is made of Teflon...

User Tools

Site Tools


cheat_sheets_python

This is an old revision of the document!


Python

Python's style guide can be found here (PEP 8).

Documentation: I find the W3School site to be more helpful initially. Then, if I need more detail and definition, I got to the official Python doc site (which, honestly, I find difficult to navigate).

W3 Schools:

Python Site: python.org

If you do not want to preface all your script names with python3 to run them, your Python scripts should being with (note: your filesystem address may differ):

#!/usr/bin/python3

Files: Reading From

Use the following syntax when opening and reading a file into a Python program:

with open('filename.txt') as file_object:
    contents = file_object.read()
    print( contents.rstrip() )
  • The open() function returns a file object (an object representing the file).
  • The keyword with closes the file once access to it is no longer needed. This structure will avoid problems…
    • You could open() and close() your file, but if if a bug in your program prevents the close() statement, the file many never close (possible data loss or corruption).
    • The structure above allows Python to automagically close the file when the time is right.
  • The read() method reads then entire file contents and stores it as a string in contents.
    • Note: read() will insert an empty string at EOF which will result in an extra blank line. To remove it, just rstrip().

File Paths: To reference files in other subdirs, you have two options…

  1. Relative File Path: the path to the file relative to the current working directory.
  2. Absolute File Path: the path to the file relative to the current file system.
# A relative file path (starts in the directory in which you started Python
with open('files/text files/filename.txt') as file_object:

# An absolute file path (b/c they are long, store them in a variable first)
file_path = '/home/greg/other_files/text_files/filename.txt'
with open(file_path) as file_object:

Web Server

Here's an easy quick Python Web Server in your pwd:

python -m SimpleHTTPServer 8080

Pick a port. 8080 is random. Tab completion works for the module name (-m is for “module”). This allows you to transfer files anywhere on the network.

  • Put your files in the subdir
  • Spin up the Python web server
  • Access that server by IP address: port number (192.168.0.101:8080) from any machine
  • Download files from the server machine to the client machine

Web Server (Py3)

If you use Python 3, you should instead write

python3 -m http.server 8080

Python3 http.server documentation: https://docs.python.org/release/3.0.1/library/http.server.html


FTP One-Liner

Note: You may have to pip install pyftpdlib

python -m pyftpdlib
  1. Ordered List ItemConnect to that machine's IP address with the port number shown.
  2. Download anything. Navigate around.

Check: File Load

Runs main() if file was not imported:

if __name__ == '__main__':
    main()

cheat_sheets_python.1672884720.txt.gz · Last modified: by gman