PythonTips
Contents
A great reference sheet is at http://rgruet.free.fr/PQR2.3.html
- = Strings =
Join items in list into string with space separators:
' '.join(list)
= List Comprehensions =Create list of integers in a range:
[i for i in range(4)]
Create a 2-D Array:
w,h = 2,3 A = [ [None]*w for i in range(h) ]
Create list of lines of output from shell command:
[os.popen('SHELL_COMMAND').read().split('\n')]List of Qualified Names:
[file[:-1] for file in os.popen(COMMAND) if pattern infile]
= Slicings =Sequence slicing [starting-at-index : but-less-than-index [ : step]]. Start defaults to 0, end to len(sequence), step to 1. a = (0,1,2,3,4,5,6,7) a[3] == 3 a[-1] == 7 a[2:4] == (2, 3) a[1:] == (1, 2, 3, 4, 5, 6, 7) a[:3] == (0, 1, 2) a[:] == (0,1,2,3,4,5,6,7) # makes a copy of the sequence. a[::2] == (0, 2, 4, 6) # Only even numbers. a[::-1] = (7, 6, 5, 4, 3 , 2, 1, 0) # Reverse order.
= File System =- os.path.isfile(FILE)
- os.path.isdir(FILE)
- (base, ext) = os.path.splitext(FILE)
Walk thru all lines of file
for line in open('FILE').readlines()- glob.glob('*.jpg')
