Tuesday, May 18, 2021

Python OS Modules

 OS 
-- 

 

`os` 

This provides a lot of functions for interacting with the operating system. 

 

>>> import os 

>>> os.getcwd()      # Return the current working directory 

'C:\\Python36' 

>>> os.chdir('/server/accesslogs')   # Change current working directory 

>>> os.system('mkdir today')   # Run the command mkdir in the system shell 

0 
 
Be sure to use `import os` rather than `from os import *` to prevent overwriting 
the builtt-in `open()` function which operates more efficiently. 
 
The built-in `dir()` and `help()` are useful aids for working with large modules like 
"os". 

 

>>> import os 

>>> dir(os) 

<returns a list of all module functions> 

>>> help(os) 

<returns an extensive manual page created from the module's docstrings> 

 

Path manipulation: 
 

>>> os.path.join('tmp', 'parentdir', 'subdir') 

'tmp/parentdir/subdir' 

>>>  

>>> os.path.abspath(os.path.join('tmp', 'parentdir', 'subdir')) 

'/tmp/tmp/parentdir/subdir' 

>>>  

>>> os.path.expanduser('~') 

'/home/merrell' 

>>>  

 

SYS 

--- 

 

`sys` 

This module also have attributes for stdin, stdout, and stderr. 

>>> sys.stderr.write('Warning, log file not found starting a new one\n') 

Warning, log file not found starting a new one 
 
It also has attribute for program termination. 

>>> import sys 

>>> sys.exit(1) 

~$  

 

SHUTIL 

------ 

 

Basics 

Used for file and directory management which provides a higher level interface that 
is easier to use. 

 

>>> import shutil 

>>> shutil.copyfile('data.db', 'archive.db') 

'archive.db' 

>>> shutil.move('/build/executables', 'installdir') 

'installdir' 

 

Deleting 

 

shutil.rmtree('/non-empty-dir')  # deletes directory recursively 

 

 

Multi-threading 

--------------- 

 

`threading` 

This allows tasks to be run in parallel on in the background. 

import threading, zipfile 

 

class AsyncZip(threading.Thread): 

    def __init__(self, infile, outfile): 

        threading.Thread.__init__(self) 

        self.infile = infile 

        self.outfile = outfile 

 

    def run(self): 

        f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) 

        f.write(self.infile) 

        f.close() 

        print('Finished background zip of:', self.infile) 

 

background = AsyncZip('mydata.txt', 'myarchive.zip') 

background.start() 

print('The main program continues to run in foreground.') 

 

background.join()    # Wait for the background task to finish 

print('Main program waited until background was done.') 
 

Use `queue` module to feed the thread with requests from other threads. 

 

Memory Management 

----------------- 

 

`weakref` 

Tracks an object without creating a reference. 
 

>>> import weakref, gc 

>>> class A: 

...     def __init__(self, value): 

...         self.value = value 

...     def __repr__(self): 

...         return str(self.value) 

... 

>>> a = A(10)                   # create a reference 

>>> d = weakref.WeakValueDictionary() 

>>> d['primary'] = a            # does not create a reference 

>>> d['primary']                # fetch the object if it is still alive 

10 

>>> del a                       # remove the one reference 

>>> gc.collect()                # run garbage collection right away 

0 

>>> d['primary']                # entry was automatically removed 

Traceback (most recent call last): 

  File "<stdin>", line 1, in <module> 

    d['primary']                # entry was automatically removed 

  File "C:/python36/lib/weakref.py", line 46, in __getitem__ 

    o = self.data[key]() 

KeyError: 'primary' 

No comments:

Post a Comment