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
Create a new virtual environment for every Python project you work on ( read).
Installation: To use virtual environments, install the python3-venv
package.
Create a Virtual Environment: Make a new directory, cd into it, create the environment, and activate it.
$ mkdir dir_name_whatever $ cd dir_name_whatever $ python3 -m venv name_of_venv # -m : tells python to run the module as a script # venv : the module we want to run as a script # name : name it whatever you want $ source name_of_venv/bin/activate # runs the script to activate your virt env # your prompt should change: prefixed by name of venv $ deactivate # exit the venv
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() )
open()
function returns a file object (an object representing the file).with
closes the file once access to it is no longer needed. This structure will avoid problems…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).read()
method reads then entire file contents and stores it as a string in contents
.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…
# 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:
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.
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
Note: You may have to pip install pyftpdlib
python -m pyftpdlib
Runs main()
if file was not imported:
if __name__ == '__main__': main()
By including this if
statement around your main program (i.e., not your imports, functions, classes, etc.), you can utilize your code in two ways:
main
and therefore the main block is executed.main
will refer to the other program into which you imported your code. Your imported program will act as a Python module and keep is own name (without the extension).