Skip to content

Instantly share code, notes, and snippets.

@guestl
guestl / loadbinlambda.py
Last active February 25, 2017 11:26
Load file in binary mode with lambda function
with codecs.open(filename, "rb", "utf-8") as file:
for ch in iter(lambda: file.read(1), ""):
print(ch)
@guestl
guestl / gist:f59fc358a439d9cad4079dc4f2c6bbe0
Created February 23, 2017 16:20
split filename to name and extension
out_name, f_ext = os.path.splitext(walker_parms['out_fn'])
@guestl
guestl / gist:a035aa502b5d6411c54216244494fd34
Created February 23, 2017 16:20
Check wether no arguments in command line for the script
if not len(sys.argv) > 1:
# There are no command-line args
if 'win' in sys.platform:
default_app_dir_for_scan = 'c:\\'
else:
default_app_dir_for_scan = '~' # check for linux
@guestl
guestl / logging.py
Last active February 23, 2017 16:21
simple example of logging
logging.basicConfig(filename='walker.log',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(lineno)d - %(message)s')
logging.debug('#' * 50)
logging.debug('---- --- start processing params ---- ---')
logging.debug('sys.platform is %s' % sys.platform)
@guestl
guestl / config_reader.py
Created February 23, 2017 16:18
read config file
def get_parms_from_conf_file(conf_file_name, default_parms):
config = configparser.ConfigParser()
try:
config.read(conf_file_name)
except Exception as e:
print("Error reading {} file".format(conf_file_name))
logging.error("Error reading %s file" % conf_file_name)
raise e
@guestl
guestl / load_file.py
Last active February 23, 2017 16:22
load file string by string to list (utf-8 compatible)
def load_custom_directories_list_from_file(cdl_filename):
if os.path.isfile(cdl_filename):
try:
with codecs.open(cdl_filename, 'r', 'utf-8') as f:
cdl_file_content = f.readlines()
# remove special chars like `\n` at the end of each line
cdl_file_content = [x.strip() for x in cdl_file_content]
return cdl_file_content
except Exception as e:
@guestl
guestl / show_wait.py
Created February 23, 2017 16:17
Rotated "wait" symbol
def show_wait(text, i):
if (i % 4) == 0:
print(text + '%s' % ("/"), end='\r')
elif (i % 4) == 1:
print(text + '%s' % ("-"), end='\r')
elif (i % 4) == 2:
print(text + '%s' % ("\\"), end='\r')
elif (i % 4) == 3:
print(text + '%s' % ("|"), end='\r')
elif i == -1:
@guestl
guestl / get_file_list_in_dir.py
Created February 23, 2017 16:16
Scan directories list
def get_file_list_in_dir(dir_name, ext_list):
result_list = []
tree = os.walk(dir_name)
i = 0
logging.debug('get_file_list_in_dir(). dir_name is %s' % (dir_name))
for d, dirs, files in tree:
for file in files:
@guestl
guestl / file open.py
Created February 23, 2017 15:24
open file for writing From https://greppage.com/tejal29/26
with open('workfile', 'w') as f:
f.write("Override existing file if exists or create new")
with open('workfile', 'a') as f:
f.write("append to existsing file if exists or create new")