Skip to content

Python split using space vs none separator

wordpress meta

title: 'Python Split Using Space vs None Separator'
date: '2014-02-12T07:10:35-06:00'
status: publish
permalink: /python-split-using-space-vs-none-separator
author: admin
excerpt: ''
type: post
id: 559
category:
    - Python
tag: []
post_format: []

Quick reminder of how to split a "ls -l" listing. It is problematic because the last field which is the directory or file name can have spaces itself, so splitting on spaces which is the only option here still need a bit of additional work.

import config
import subprocess

## First experiment doing manual manipulation of the fields.  
## Basically split on all spaces and then assemble the file name.
def dir_listing():
  ls_lines = subprocess.check_output(['ls', '-l']).splitlines()
  ls_arr= []
  for item in ls_lines:
    if not "total" in item:
      f = item.split()
      ## fname = item.join(str(v) for v in item.index if v > 7)
      fname = ""
      for idx,val in enumerate(f):
        if idx > 7:
          fname = fname + str(val) + " "
      fname = fname[:-1]
      ls_fields = [f[0],f[1],f[2],f[3],f[4],f[5]+"-"+f[6]+"-"+f[7],fname]
      ls_arr.append(ls_fields)
  return ls_arr

## Second attempt is split on spaces with a max split defined. 
## This sounds like the obvious way to do this but I needed to use "None" and not " "
## as shown below.
def dir_listing_optimized():
  ls_lines = subprocess.check_output(['ls', '-l']).splitlines()
  ls_arr= []
  for item in ls_lines:
    if not "total" in item:
      ## None will use arbitrary strings of whitespace characters (space, tab, newline, return, formfeed)
      ## When using split(" ",8) I had more separation than I expected and therefor my last field 
      ## was not working right.
      f = item.split(None,8)
      ls_arr.append(f)
  return ls_arr

for dir in dir_listing():
  print dir

for dir in dir_listing_optimized():
  print dir