Skip to content

Python quick and dirty tricks

Zakir Syed edited this page May 3, 2019 · 7 revisions

File IO


Create a folder in CWD(Current working directory)

import sys, os
my_dir = os.path.join(os.getcwd(), 'temp')
if not os.path.isdir(my_dir):
    os.makedirs(my_dir)

Get all files in a dir with a pattern in filename

import glob
my_files = glob.glob(my_dir+ '/' +"log_*.txt")

Rename all files in current directory replacing all ' ' and '-' by '_'

for filename in os.listdir(os.path.dirname(os.path.abspath(__file__))):
after = filename
after = after.replace(" ", "_"); after = after.replace("-", "_");
print(f"Before: {filename} After: {after}")
    os.rename(filename, after)

Sort filenames in natural order

files = [log_1.txt, log_54.txt, log_2.txt, log_5.txt log_11.txt, log_12.txt]  

Python's In-built sort is lexicographical order i.e dictionary order

sorted(flies)
[log_1.txt, log_11.txt, log_12.txt, log_2.txt, log_5.txt, log_55.txt ]

To achieve natural order i.e [log_1.txt, log_2.txt, log_5.txt, log_11.txt, log_12.txt, log_55.txt]
Extract the number from the file name and sort by it. Solution using lambda:-

pdf_files.sort(key=lambda x: int(x[x.find('_')+1 : x.find('.')]))  

C++ example see this natural_order_sort.cpp


Splitting Strings


Split a string on multiple characters say comma(','), space(' ') and hiphen('-') etc..

import import re  
filter(None, re.split("[, \-!?]+", "Hey, you - what are you doing here!?"))

Output list: ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']

Where:

  • the […] matches one of the separators listed inside,
  • the - in the regular expression is here to prevent the special interpretation of - as a character range indicator (as in A-Z),
  • the + skips one or more delimiters (it could be omitted thanks to the filter(), but this would unnecessarily produce empty strings between matched separators), and
  • filter(None, …) removes the empty strings possibly created by leading and trailing separators (since empty strings have a false boolean value).