Raw Strings (I Think)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lanny

    Raw Strings (I Think)

    I've used glob.glob to get a list of files in a directory
    and now I want to use os.system to execute one of
    those files, the problem is that python automatically
    puts a escape charater infront of the back slashes
    so the os.system gets X:\\####\\####\ \ and is useless,
    I think I need to convert my string to a raw string but
    I don't know how.



    -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --
  • Mike Driscoll

    #2
    Re: Raw Strings (I Think)

    On Jul 24, 10:02 am, "Lanny" <la...@freshell s.chwrote:
    I've used glob.glob to get a list of files in a directory
    and now I want to use os.system to execute one of
    those files, the problem is that python automatically
    puts a escape charater infront of the back slashes
    so the os.system gets X:\\####\\####\ \ and is useless,
    I think I need to convert my string to a raw string but
    I don't know how.
    >
    -- Posted on news://freenews.netfront.net - Complaints to n...@netfront.n et --
    This works fine for me on Windows XP.

    I did this:

    glob.glob(r'c:\ test')

    which gave me something like this:

    ['c:\\test\\07-24TimeSheet.xls ', 'c:\\test\\acct s.CSV', 'c:\\test\
    \Ataris Aqu\xe1ticos #2.txt', 'c:\\test\\chan ges.txt', 'c:\\test\
    \change_g.txt', 'c:\\test\\conf ig.ini', 'c:\\test\\Coun ty.txt', 'c:\
    \test\\county1. txt', 'c:\\test\\ctyp es-1.0.1.tar.gz', 'c:\\test\
    \DAMNATUS_Sound track.zip', 'c:\\test\\dood ad.1.12.3.doc', 'c:\\test\
    \doodad.1.32.3. doc', 'c:\\test\\dood ad.22.12.3.doc' , 'c:\\test\
    \emailMess.xml' , 'c:\\test\\Eula .txt', 'c:\\test\\fast a.txt', 'c:\\test
    \\Funds.txt', 'c:\\test\\Fund s2.txt', 'c:\\test\\Gman e.newsrc', 'c:\
    \test\\groups.i ni', 'c:\\test\\hamm y.doc']

    Now, if I use os.system like this, it works:

    os.system('note pad %s' % x[1])

    This opens notepad with my *.csv file just fine. Windows XP, Python
    2.5.2.

    Mike

    Comment

    • Carsten Haese

      #3
      Re: Raw Strings (I Think)

      Lanny wrote:
      I've used glob.glob to get a list of files in a directory
      and now I want to use os.system to execute one of
      those files, the problem is that python automatically
      puts a escape charater infront of the back slashes
      No, it doesn't. Instead of guessing what the cause might be, please show
      us your code and show us the error message you're getting, so that we
      can determine what the cause really is.

      --
      Carsten Haese

      Comment

      • Fredrik Lundh

        #4
        Re: Raw Strings (I Think)

        Lanny wrote:
        I've used glob.glob to get a list of files in a directory
        and now I want to use os.system to execute one of
        those files, the problem is that python automatically
        puts a escape charater infront of the back slashes
        so the os.system gets X:\\####\\####\ \ and is useless,
        No, it doesn't. The backslash doubling only happens when you "echo" a
        variable to the terminal in interactive mode. If you want to know that
        the string really contains, use "print".

        Here's an example:
        >>import glob
        >>files = glob.glob("\\bi n\\ls.exe")
        >>files
        ['\\bin\\ls.exe']
        >>files[0]
        '\\bin\\ls.exe'
        >>print files[0]
        \bin\ls.exe
        >>import os
        >>os.system(fil es[0])
        Demo Makefile.pre.in Parser
        Doc Misc Python
        Grammar Modules README
        Include Objects RISCOS
        LICENSE PC Tools
        Lib PCbuild configure
        Mac PCbuild8 configure.in

        Btw, if you want to pass arguments to the program, you might want to use
        the "subprocess " module instead, since it handles escaping and quoting
        for you all by itself:
        >>import subprocess
        >>subprocess.ca ll([files[0], "-l"])
        total 479
        drwxr-xr-x 26 1006 everyone 0 Oct 14 2006 Demo
        drwxr-xr-x 33 1006 everyone 0 Oct 14 2006 Doc
        drwxr-xr-x 6 1006 everyone 0 Oct 14 2006 Grammar
        ....

        There's also a function called "os.startfi le", which can be used to
        "open" an arbitrary file (in the same as if you'd double-click on it in
        the explorer).
        I think I need to convert my string to a raw string but
        I don't know how.
        Raw strings are an alternate syntax for adding string literals to your
        source code, and has nothing to do with output.

        </F>

        Comment

        • Lanny

          #5
          Re: Raw Strings (I Think)

          No, it doesn't. Instead of guessing what the cause might be, please show
          us your code and show us the error message you're getting, so that we can
          determine what the cause really is.
          Ok, sorry. Heres my code:

          import glob
          import random
          import os

          songs = glob.glob('C:\# ##\###\###\*.mp 3')
          pick = random.choice(s ongs)
          os.system(pick)

          And yes, I know there are better ways of randomly selecting
          a .mp3 file to play but I don't care.



          -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --

          Comment

          • Fredrik Lundh

            #6
            Re: Raw Strings (I Think)

            Lanny wrote:
            >No, it doesn't. Instead of guessing what the cause might be, please show
            >us your code and show us the error message you're getting, so that we can
            >determine what the cause really is.
            >
            Ok, sorry. Heres my code:
            >
            import glob
            import random
            import os
            >
            songs = glob.glob('C:\# ##\###\###\*.mp 3')
            pick = random.choice(s ongs)
            os.system(pick)
            >
            And yes, I know there are better ways of randomly selecting
            a .mp3 file to play but I don't care.
            my guess is that the real problem is that you get back filenames with
            spaces in them, which gets treated as multiple arguments by os.system.

            using os.startfile will fix this:
            >>import glob, os, random
            >>file = random.choice(g lob.glob("\\mus ic\\*.mp3"))
            >>file
            '\\music\\Madru gada - Grit - 05 - Seven Seconds.mp3'
            >>print file
            \music\Madrugad a - Grit - 05 - Seven Seconds.mp3
            >>os.system(fil e)
            '\music\Madruga da' is not recognized as an internal or external command,
            operable program or batch file.
            1
            >>os.startfile( file)
            .... music starts playing ...

            </F>

            Comment

            • Carsten Haese

              #7
              Re: Raw Strings (I Think)

              Lanny wrote:
              >No, it doesn't. Instead of guessing what the cause might be, please show
              >us your code and show us the error message you're getting, so that we can
              >determine what the cause really is.
              >
              Ok, sorry. Heres my code:
              [...]
              And the error message you're getting is...?

              --
              Carsten Haese

              Comment

              • Lanny

                #8
                Re: Raw Strings (I Think)

                my guess is that the real problem is that you get back filenames with
                spaces in them, which gets treated as multiple arguments by os.system.
                >
                using os.startfile will fix this:
                >
                >import glob, os, random
                >file = random.choice(g lob.glob("\\mus ic\\*.mp3"))
                >file
                '\\music\\Madru gada - Grit - 05 - Seven Seconds.mp3'
                >print file
                \music\Madrugad a - Grit - 05 - Seven Seconds.mp3
                >os.system(file )
                '\music\Madruga da' is not recognized as an internal or external command,
                operable program or batch file.
                1
                >os.startfile(f ile)
                ... music starts playing ...
                >
                </F>
                Thanks I just switched the startfile for system and it worked like a charm
                Thanks



                -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --

                Comment

                • Gabriel Genellina

                  #9
                  Re: Raw Strings (I Think)

                  En Thu, 24 Jul 2008 12:02:00 -0300, Lanny <lanny@freshell s.chescribió:
                  I've used glob.glob to get a list of files in a directory
                  and now I want to use os.system to execute one of
                  those files, the problem is that python automatically
                  puts a escape charater infront of the back slashes
                  so the os.system gets X:\\####\\####\ \ and is useless,
                  I think I need to convert my string to a raw string but
                  I don't know how.
                  Those \\ represent a SINGLE character. That is, they LOOK duplicated in code and when you use repr(...) but it's actually a single backslash:
                  >>path = "X:\\abc"
                  >>path
                  'X:\\abc'
                  >>print path
                  X:\abc
                  >>len(path)
                  6

                  Probably you have another issue - please post a short but complete failing code...

                  --
                  Gabriel Genellina

                  Comment

                  Working...