open file with whitespaces

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

    #1

    open file with whitespaces

    Hi guys.
    I've a very big big big problem:

    I've in my windows computer a file named cicciobello.htm l, located in
    c:\documents and settings\userna me\desktop\cicc iobello.html.

    Now, I MUST open this file with os.spawn(os.P_W AIT ...., because I must
    wait the user cancel the explorer window, ok?
    And so, I write:

    import os
    os.spawnl(os.P_ WAIT, "c:\programmi\i nternet explorer\iexplo rer.exe",
    "c:\documen ts and settings\userna me\desktop\cicc iobello.html")

    the python process don't fail, but explorer don't visualize correctly
    the file opened: i receive an "not found" error message.

    I've found that the whitespaces in file path are the problem.
    If you see to explorer address bar, you will find the address
    completely wrong...

    I've found no solution!
    Can you help me, please??

    thx very very very much!!!

  • Christophe

    #2
    Re: open file with whitespaces

    mardif a écrit :[color=blue]
    > Hi guys.
    > I've a very big big big problem:
    >
    > I've in my windows computer a file named cicciobello.htm l, located in
    > c:\documents and settings\userna me\desktop\cicc iobello.html.
    >
    > Now, I MUST open this file with os.spawn(os.P_W AIT ...., because I must
    > wait the user cancel the explorer window, ok?
    > And so, I write:
    >
    > import os
    > os.spawnl(os.P_ WAIT, "c:\programmi\i nternet explorer\iexplo rer.exe",
    > "c:\documen ts and settings\userna me\desktop\cicc iobello.html")
    >
    > the python process don't fail, but explorer don't visualize correctly
    > the file opened: i receive an "not found" error message.
    >
    > I've found that the whitespaces in file path are the problem.
    > If you see to explorer address bar, you will find the address
    > completely wrong...
    >
    > I've found no solution!
    > Can you help me, please??
    >
    > thx very very very much!!!
    >[/color]

    And I thought the problem where the incorrectly used \ :) Try that first :

    os.spawnl(os.P_ WAIT, r"c:\programmi\ internet explorer\iexplo rer.exe",
    r"c:\documen ts and settings\userna me\desktop\cicc iobello.html")

    Comment

    • bruno at modulix

      #3
      Re: open file with whitespaces

      mardif wrote:[color=blue]
      > Hi guys.
      > I've a very big big big problem:[/color]

      <ot>
      I think a lot of people in the world would not find it so big wrt/ their
      own situation...
      </ot>
      [color=blue]
      > I've in my windows computer a file named cicciobello.htm l, located in
      > c:\documents and settings\userna me\desktop\cicc iobello.html.
      >
      > Now, I MUST open this file with os.spawn(os.P_W AIT ...., because I must
      > wait the user cancel the explorer window, ok?
      > And so, I write:
      >
      > import os
      > os.spawnl(os.P_ WAIT, "c:\programmi\i nternet explorer\iexplo rer.exe",
      > "c:\documen ts and settings\userna me\desktop\cicc iobello.html")[/color]

      take care of the meaning of the antislash in strings... What do you
      thing "c:\toto" or "c:\nothing " will expand to ?-)

      import os.path
      help(os.path)

      Or at least, and IIRC, "c:/something/" should work.
      [color=blue]
      > the python process don't fail, but explorer don't visualize correctly
      > the file opened: i receive an "not found" error message.
      >
      > I've found that the whitespaces in file path are the problem.
      > If you see to explorer address bar, you will find the address
      > completely wrong...[/color]

      Not using Windows, I won't find anything. So what's the result ?-)

      Anyway, you may find urllib.urlencod e useful for, well, encoding urls...

      HTH
      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • Claudio Grondi

        #4
        Re: open file with whitespaces

        Christophe wrote:[color=blue]
        > mardif a écrit :
        >[color=green]
        >> Hi guys.
        >> I've a very big big big problem:
        >>
        >> I've in my windows computer a file named cicciobello.htm l, located in
        >> c:\documents and settings\userna me\desktop\cicc iobello.html.
        >>
        >> Now, I MUST open this file with os.spawn(os.P_W AIT ...., because I must
        >> wait the user cancel the explorer window, ok?
        >> And so, I write:
        >>
        >> import os
        >> os.spawnl(os.P_ WAIT, "c:\programmi\i nternet explorer\iexplo rer.exe",
        >> "c:\documen ts and settings\userna me\desktop\cicc iobello.html")
        >>
        >> the python process don't fail, but explorer don't visualize correctly
        >> the file opened: i receive an "not found" error message.
        >>
        >> I've found that the whitespaces in file path are the problem.
        >> If you see to explorer address bar, you will find the address
        >> completely wrong...
        >>
        >> I've found no solution!
        >> Can you help me, please??
        >>
        >> thx very very very much!!!
        >>[/color]
        >
        > And I thought the problem where the incorrectly used \ :) Try that first :
        >
        > os.spawnl(os.P_ WAIT, r"c:\programmi\ internet explorer\iexplo rer.exe",
        > r"c:\documen ts and settings\userna me\desktop\cicc iobello.html")[/color]
        Another option to try in case this is not the problem is to put the file
        name in quotation marks as it is necessary when using command line shell
        (not tested, just an idea):

        os.spawnl(os.P_ WAIT,'"c:\\prog rammi\\internet explorer\\iexpl orer.exe"',
        '"c:\\docume nts and settings\\usern ame\\desktop\\c icciobello.html "')

        Comment

        • mardif

          #5
          Re: open file with whitespaces

          OK OK GUYS!!!!
          I've found the solution: ( effectly, a friend of mine has found the
          solution )

          import os

          os.spawnl(os.P_ WAIT, "c:\programmi\i nternet
          explorer\iexplo re.exe",'"C:\Do cuments and
          Settings\michel e\Desktop\cicci o.html"','"C:\D ocuments and
          Settings\michel e\Desktop\cicci o.html"')

          The secret are the ' simbols around arguments:

          ' "C:\Documen ts and Settings\michel e\Desktop\cicci o.html" '

          Without these, don't work!

          Very STRONG!!!!!

          bye and thx

          Comment

          • Fredrik Lundh

            #6
            Re: open file with whitespaces

            "mardif" wrote:
            [color=blue]
            > Now, I MUST open this file with os.spawn(os.P_W AIT ...., because I must
            > wait the user cancel the explorer window, ok?[/color]

            note that backslashes in string literals have special meaning in Python; to make
            sure a backslash in the string literal really ends up as a backslash in the resulting
            string, you must either double each backslash, or use "raw" strings:

            program = r"c:\programmi\ internet explorer\iexplo rer.exe"
            file = r"c:\documen ts and settings\userna me\desktop\cicc iobello.html"

            also, getting the quoting/escaping right with os.exec/os.spawn is quite messy. I
            recommend using the subprocess module instead:

            import subprocess
            print subprocess.call ([program, file])

            </F>



            Comment

            • Claudio Grondi

              #7
              Re: open file with whitespaces

              mardif wrote:[color=blue]
              > OK OK GUYS!!!!
              > I've found the solution: ( effectly, a friend of mine has found the
              > solution )
              >
              > import os
              >
              > os.spawnl(os.P_ WAIT, "c:\programmi\i nternet
              > explorer\iexplo re.exe",'"C:\Do cuments and
              > Settings\michel e\Desktop\cicci o.html"','"C:\D ocuments and
              > Settings\michel e\Desktop\cicci o.html"')
              >
              > The secret are the ' simbols around arguments:
              >
              > ' "C:\Documen ts and Settings\michel e\Desktop\cicci o.html" '
              >
              > Without these, don't work!
              >
              > Very STRONG!!!!!
              >
              > bye and thx
              >[/color]
              Wasn't that what I have suggested?

              By the way:
              it is much better to use double backslashes here, as in case of other
              file/directory names (e.g. folder\name.txt folder\remote.h tm i.e. for
              all the cases a backslash is followed by a letter making out of this
              twin one special character) this above won't work.
              But best is to use in full path file names forward slashes as Python
              handles them properly not only on *nix and Linux, but also on Windows:
              '"C:/Documents and Settings/michele/Desktop/ciccio.html"'

              The "secret" symbols around arguments are single quotation marks making
              the double quotation marks part of the string passed to the .spawnl()
              function. Check out in IDLE, that:[color=blue][color=green][color=darkred]
              >>> "x"[/color][/color][/color]
              'x'[color=blue][color=green][color=darkred]
              >>> '"x"'[/color][/color][/color]
              '"x"'
              Command shell command arguments need to be enclosed in quotation marks
              in case spaces can occur in them (as it can happen in file names) as
              otherwise the spaces will be interpreted as separator characters between
              the arguments and as consequence of this the command shell command fails
              due to bad or wrong number of parameter.

              You see, no "secrets" here ... only some simple rules and a bit more
              understanding of what is going on behind the scenes.

              Claudio

              Comment

              Working...