Re: os.unlink on Windows

Discussion in 'Python' started by Thomas Jollans, Aug 7, 2010.

  1. On 08/07/2010 01:10 PM, Shambhu Sharma wrote:
    > Hi,
    >
    > I am new to Python. I was trying to use os.unlink function in
    > windows. But i am getting error:
    > OSError: [Errno 2] No such file or directory:
    > 'C:\\SHAMBHU\\tmp\\text_delete.txt'
    >
    > Input file to os.unlink is: 'C:\SHAMBHU\tmp\text_delete.txt'. But
    > os.unlink is adding extra backslash with pathname.


    No, it isn't. What you're seeing is simply the repr() of the path name
    string.

    >>> p = r'C:\SHAMBHU\tmp\text_delete.txt'
    >>> p

    'C:\\SHAMBHU\\tmp\\text_delete.txt'
    >>> print(p)

    C:\SHAMBHU\tmp\text_delete.txt
    >>>



    I think the file you're trying to delete probably doesn't exist. Why
    don't you double-check that.



    > I tried with
    > Python2.5 and Python3.1 but got same error.
    > Please suggest how to remove this error.
    >
    > --
    > If linux doesn't have a solution, then u have a wrong problem.
    >
    > Shambhu Kumar Sharma
    > 91-98864 91913
    >
     
    Thomas Jollans, Aug 7, 2010
    #1
    1. Advertising

  2. Thomas Jollans

    Shambhu Guest

    Hi Thomas,

    I checked, file is present. Here is my sample script:
    import os
    filename = "C:\SHAMBHU\tmp\text_delete.txt"
    os.unlink(filename)

    File "C:\SHAMBHU\tmp\text_delete.txt" is accessible but "C:\\SHAMBHU\
    \tmp\\text_delete.txt" is not (with extra backslash in path which is
    added by os.unlink).

    Regards.
    Shambhu.



    On Aug 7, 4:46 pm, Thomas Jollans <> wrote:
    > On 08/07/2010 01:10 PM, Shambhu Sharma wrote:
    >
    > > Hi,

    >
    > >         I am new to Python. I was trying to use os.unlink function in
    > > windows. But i am getting error:
    > > OSError: [Errno 2] No such file or directory:
    > > 'C:\\SHAMBHU\\tmp\\text_delete.txt'

    >
    > > Input file to os.unlink is: 'C:\SHAMBHU\tmp\text_delete.txt'. But
    > > os.unlink is adding extra backslash with pathname.

    >
    > No, it isn't. What you're seeing is simply the repr() of the path name
    > string.
    >
    > >>> p = r'C:\SHAMBHU\tmp\text_delete.txt'
    > >>> p

    >
    > 'C:\\SHAMBHU\\tmp\\text_delete.txt'>>> print(p)
    >
    > C:\SHAMBHU\tmp\text_delete.txt
    >
    >
    >


    > I think the file you're trying to delete probably doesn't exist. Why
    > don't you double-check that.
    >
    > > I tried with
    > > Python2.5 and Python3.1 but got same error.
    > > Please suggest how to remove this error.

    >
    > > --
    > > If linux doesn't have a solution, then u have a wrong problem.

    >
    > > Shambhu Kumar Sharma
    > > 91-98864 91913

    >
    >
     
    Shambhu, Aug 8, 2010
    #2
    1. Advertising

  3. On 08/08/2010 01:41 PM, Shambhu wrote:
    > Hi Thomas,
    >
    > I checked, file is present. Here is my sample script:
    > import os
    > filename = "C:\SHAMBHU\tmp\text_delete.txt"
    > os.unlink(filename)
    >
    > File "C:\SHAMBHU\tmp\text_delete.txt" is accessible but "C:\\SHAMBHU\
    > \tmp\\text_delete.txt" is not (with extra backslash in path which is
    > added by os.unlink).


    os.unlink isn't adding anything. The extra backslashes you're seeing are
    being added when DISPLAYING the file name string. That's because Python
    uses backslashes to escape special characters in strings.

    >>> print("C:\\SHAMBHU\\tmp\\text_delete.txt")

    C:\SHAMBHU\tmp\text_delete.txt
    >>> print("C:\SHAMBHU\tmp\text_delete.txt")

    C:\SHAMBHU mp ext_delete.txt
    >>>


    '\t' is the TAB character.

    What is the error message, exactly, when you run the above script? Since
    you didn't escape the backslashes properly, I expect it refers to
    'C:\\SHAMBHU\tmp\text_delet.txt' -- The first backslash is still a
    backslash -- it gets doubled when printed as a string (but NOT when
    passed to the OS by unlink), but the second and third ones were used to
    create TAB characters. Probably.

    Either always double backslashes in paths, or use raw string literals
    (r"C:\SHAMBHU\tmp\text_delete.txt" -- keeps the \s) (or use forward
    slashes in paths...)

    >
    > Regards.
    > Shambhu.
    >
    >
    >
    > On Aug 7, 4:46 pm, Thomas Jollans <> wrote:
    >> On 08/07/2010 01:10 PM, Shambhu Sharma wrote:
    >>
    >>> Hi,

    >>
    >>> I am new to Python. I was trying to use os.unlink function in
    >>> windows. But i am getting error:
    >>> OSError: [Errno 2] No such file or directory:
    >>> 'C:\\SHAMBHU\\tmp\\text_delete.txt'

    >>
    >>> Input file to os.unlink is: 'C:\SHAMBHU\tmp\text_delete.txt'. But
    >>> os.unlink is adding extra backslash with pathname.

    >>
    >> No, it isn't. What you're seeing is simply the repr() of the path name
    >> string.
    >>
    >>>>> p = r'C:\SHAMBHU\tmp\text_delete.txt'
    >>>>> p

    >>
    >> 'C:\\SHAMBHU\\tmp\\text_delete.txt'>>> print(p)
    >>
    >> C:\SHAMBHU\tmp\text_delete.txt
    >>
    >>
    >>

    >
    >> I think the file you're trying to delete probably doesn't exist. Why
    >> don't you double-check that.
    >>
    >>> I tried with
    >>> Python2.5 and Python3.1 but got same error.
    >>> Please suggest how to remove this error.

    >>
    >>> --
    >>> If linux doesn't have a solution, then u have a wrong problem.

    >>
    >>> Shambhu Kumar Sharma
    >>> 91-98864 91913

    >>
    >>

    >
     
    Thomas Jollans, Aug 8, 2010
    #3
  4. On Sun, 08 Aug 2010 04:41:14 -0700, Shambhu wrote:

    > Hi Thomas,
    >
    > I checked, file is present. Here is my sample script:
    > import os
    > filename = "C:\SHAMBHU\tmp\text_delete.txt"


    Did you intend to provide a filename with two TAB characters in it?

    c colon backslash s h a m b u TAB m p TAB e x t underscore d e l e t e
    dot t x t

    > File "C:\SHAMBHU\tmp\text_delete.txt" is accessible but "C:\\SHAMBHU\
    > \tmp\\text_delete.txt" is not (with extra backslash in path which is
    > added by os.unlink).


    os.unlink does add any extra backslashes. You are misinterpreting what
    you are seeing.



    --
    Steven
     
    Steven D'Aprano, Aug 8, 2010
    #4
  5. Thomas Jollans

    Shambhu Guest

    Hi Thomas, Steven,
    Thanks for explanation. It is working now after
    using double backslash in pathname. I was misinterpreting the display
    output and thinking that it was being added by 'os' module.

    Regards,
    Shambhu.

    T
    On Aug 8, 6:29 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Sun, 08 Aug 2010 04:41:14 -0700, Shambhu wrote:
    > > Hi Thomas,

    >
    > >            I checked, file is present. Here is my sample script:
    > > import os
    > > filename = "C:\SHAMBHU\tmp\text_delete.txt"

    >
    > Did you intend to provide a filename with two TAB characters in it?
    >
    > c colon backslash s h a m b u TAB m p TAB e x t underscore d e l e t e
    > dot t x t
    >
    > > File "C:\SHAMBHU\tmp\text_delete.txt" is accessible but "C:\\SHAMBHU\
    > > \tmp\\text_delete.txt" is not (with extra backslash in path which is
    > > added by os.unlink).

    >
    > os.unlink does add any extra backslashes. You are misinterpreting what
    > you are seeing.
    >
    > --
    > Steven
     
    Shambhu, Aug 9, 2010
    #5
  6. In message
    <>, Shambhu
    wrote:

    > It is working now after using double backslash in pathname.


    Might be simpler to use slashes.
     
    Lawrence D'Oliveiro, Aug 9, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Urbanus
    Replies:
    0
    Views:
    2,272
    Paul Urbanus
    Apr 7, 2006
  2. Coffee Pot

    How can I unlink/delete an open file in Windows?

    Coffee Pot, Oct 18, 2008, in forum: C Programming
    Replies:
    12
    Views:
    725
    CBFalconer
    Oct 20, 2008
  3. Replies:
    4
    Views:
    229
    Paul Battley
    Jul 6, 2007
  4. Nicholas Manning

    tempfile.rb and unlink on windows

    Nicholas Manning, May 19, 2009, in forum: Ruby
    Replies:
    2
    Views:
    240
    Nicholas Manning
    May 19, 2009
  5. Replies:
    12
    Views:
    541
Loading...

Share This Page