Results 1 to 3 of 3

Thread: Python and Double Backslashes in Windows

  1. #1
    Join Date
    Feb 2006
    Location
    Kentucky
    Beans
    571
    Distro
    Ubuntu

    Python and Double Backslashes in Windows

    So as some of you may know, in Python backslashes are escape characters, so if you want to actually print a backslash in a string, you just write two backslashes next to one another. Normally I do this all the time when pointing to folder paths in Windows and have no problems.

    However, being a wee nooblet at python, I decided to convert one of my bash scripts to python so it would work on Windows. The very last thing that happens is it writes the contents of a variable to a text file. The section almost identical to this that runs "if os.name is 'posix'", runs just fine on my linux box. However, on Windows when this block of code tries to run, I get an error basically showing me that it's keeping the double backslashes. I've tried typing single backslashes, I've tried doing var.replace('\\\\', '\\') to get rid of them, didn't work. I really don't quite know what to do.

    The error I'm getting is: OSError: [Errno 22] Invalid argument: 'C:\\Users\\Marcus\\Desktop\\checkmd5-results-2013-10-23 01:17:21.376880.txt'

    Code:
    if os.name is "nt":
         dest = open ( os.environ [ 'USERPROFILE' ] + "\\Desktop\\checkmd5-results-%s.txt"%(date), "w" )
         dest.write ( date + "\n\n" )
         dest.write ( "This file generated by Marcus Adams's CheckMD5 " + ver + "\n\n" )
         dest.write ( save2 )
         dest.close()
         msgbox( msg = "Results saved to:\n checkmd5-results-%s.txt\n\nClick OK to exit."%(date), title = ver)
    "Civilization is the limitless multiplication of unnecessary necessities."
    -- Mark Twain

  2. #2
    Join Date
    Feb 2006
    Location
    Kentucky
    Beans
    571
    Distro
    Ubuntu

    Re: Python and Double Backslashes in Windows

    Apparently the issue was in the fact that the time-stamp contained colons :, so I just did a var replace on the date to convert them to decimals and it works just fine now.
    Last edited by gerowen; October 23rd, 2013 at 10:01 AM.
    "Civilization is the limitless multiplication of unnecessary necessities."
    -- Mark Twain

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Python and Double Backslashes in Windows

    In python you can use "raw" string literals, where a single `\' is considred a mere '\' (except before a quote of the same type as the opening quote)
    Code:
    dest = open ( os.environ [ 'USERPROFILE' ] + r'\Desktop\checkmd5-results-%s.txt' % (date), "w" )
    This can make a few things more readable, and avoids the leaning toothpicks syndrome when writing regular expressions.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •