How to split with "\" character, and licence copyleft mirror of ©

Tim Roberts timr at probo.com
Sun Sep 1 22:40:01 EDT 2013


materile11 at gmail.com wrote:

>Hello everybody
>I'm trying to run this: 
>
><code>
>>>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
>>>> a.split('\')
>
>SyntaxError: EOL while scanning string literal
></code>
>
>I think that the character '\' is the problem, but unfortunately I'm
>developing a small app for windows and I need to show only the name
>of the .wav file, in this case 'flute.wav'.

I assume you know that backslash has a special meaning in string constants.
For example the string '\n\r' contains exactly two characters, and no
backslashes.

When you want to use an actual backslash in an ordinary string constant,
you have to double it.  So, you could have written your code as:
    a = 'E:\\Dropbox\\jjfsdjjsdklfj\\sdfjksdfkjslkj\\flute.wav'
    a.split('\\')

Another altrnative is to use "raw" strings, in which backslashes are not
interpreted:
    a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
    a.split(r'\')

I assume your filename is actually input to your program, and not as a
constant in your code, so that may not be a problem.  However, there is an
API to do exactly what you're asking:

  >>> import os
  >>> a=r'E:\Dropbox\one\two\three\flute.wav'
  >>> os.path.split(a)
  ('E:\\Dropbox\\one\\two\\three', 'flute.wav')
  >>> os.path.split(a)[1]
  'flute.wav'
  >>>
  
>I also want to know how to mirror a character, in my case this one ©,
>because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft
>to distribute my app.

You can't "mirror" a character.  That is an invented glyph that is not
present in Unicode.  Fortunately, the character doesn't have any legal
meaning, so you can just include explanatory text in your description that
identifies your license.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list