1

I have a Python file which calls a case sensitive sorting routine provided by the underlying OS. This program was originally tested in Unix.

The code fragment looks as follows:

def sort(path, filename, args=''):
   s = 'LC_ALL=C sort -S 50% --parallel=4 {0} {1} -o {1}'
   status = subprocess.call(s.format(args, os.path.join(path, filename)), shell=True)
   if status != 0:
      raise Exception('unable to sort file: {}'.format(filename))

However, running this program in Windows throws the error

"LC_ALL=C :Command not found"

and the default "sort" routine in Windows is case sensitive.

Is there any corresponding case sensitive sort routine that I can call in Windows or modify this command to remove this issue?

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
Rohit
  • 59
  • 5

1 Answers1

1

In Unix, LC_ALL is the environment variable that overrides localisation settings. You can override localisation settings in the Windows sort command with the /L flag.

Try the following. I didn't test it. The Windows sort command is put together based on the documentation.
Also, for platform determination, take a look at How can I find the current OS in Python? [duplicate].

import os
import sys
import subprocess


def sort(path, filename, args=''):
    if 'win' in sys.platform.lower():
        s = 'sort /L /C {0} /o {1}'
    else:
        s = 'LC_ALL=C sort -S 50% --parallel=4 {0} {1} -o {1}'
    status = subprocess.call(s.format(args, os.path.join(path, filename)), shell=True)
    if status != 0:
      raise Exception('unable to sort file: {}'.format(filename))
Community
  • 1
  • 1
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
  • its still not case-sensitive. Even on the documentation link you provided, it says "The sort is always case insensitive". I need to do case sensitive sorting – Rohit Jan 24 '16 at 01:24
  • 1
    You have to specify the "C" locale and input file, plus the `args` isn't portable, so you can probably just ignore it: `filepath = os.path.join(path, filename);` `s = 'sort.exe /C /L C "{0}" /o "{0}"'.format(filepath);` `subprocess.check_call(s)`. – Eryk Sun Jan 24 '16 at 04:09
  • 1
    The `/C` option for a case sensitive sort is only available in the Windows 7+ version of sort.exe. Note that Windows compares lower-case letters as less than upper-case letters, unless you're using the C locale, which simply compares the code points (i.e. ASCIIbetical order). – Eryk Sun Jan 24 '16 at 04:10