• 0

Python and Double Backslashes


Question

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'

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)
Link to comment
Share on other sites

2 answers to this question

Recommended Posts

This topic is now closed to further replies.