======Python Programming Notes====== Here are my notes from starting to use and learn the python programming language. I am only focusing on Python 3 at this time. {{tag>python subprocess subprocess.run}} =====subprocess module===== The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.\\ Some key points: *The command to be run arguments "args" should be passed in as a list, the list components should not contain any white space. e.g.: * ''args=[cmd, '-I', 'lanplus', '-H', BMC_IP, "-U", User_Name, '-f', PW_file_location, 'power', power_ctl]'', where components in quotation are text and those without are variables. * With argument type the input option ''shell=False'' must be set. This is the default default and preferred option. * ''stdout=subprocess.PIPE'' In order to capture the command output from the function. Otherwise it will go to the standard output stream, e.g. the terminal screen. * the output value can be captured from ''subprocess.stdout'', this is in byte format. * It can be captured and converted to text format ''subprocess.stdout.decode('utf-8')'' * The return code of the function can be capture from output using ''subprocess.returncode'' =====Python Back-up Scripts===== My python based back-up scripts are located here: [[home_server:home_server_setup:other_services:back-up_server#python_backup_scripts]] =====Some on line resources===== *[[https://docs.python.org/3/library/subprocess.html#popen-objects|Python Docs - Subprocess management]] *[[https://www.geeksforgeeks.org/python-execute-and-parse-linux-commands/|Python | Execute and parse Linux commands]] *[[https://www.golinuxcloud.com/python-subprocess/|10+ practical examples to learn python subprocess module]] *[[https://stackoverflow.com/questions/49371575/print-subprocess-popen|Print Subprocess.Popen]] *[[https://www.pythonpool.com/python-bytes-to-string/|5 Ways to Convert bytes to string in Python]] *[[https://opensourceoptions.com/blog/how-to-pass-arguments-to-a-python-script-from-the-command-line/|How to Pass Arguments to a Python Script from the Command Line]] *[[https://www.golinuxcloud.com/python-write-to-file/|How to create, read, append, write to file in Python]] *[[https://www.programiz.com/python-programming/exception-handling|Python Exception Handling Using try, except and finally statement]] <- project:index|Back ^ project:index|Start page ^ project:oscilloscope|Next ->