Jump to content



Photo

Python and Double Backslashes

Answered Go to the full post python windows backslash

  • Please log in to reply
2 replies to this topic

#1 Gerowen

Gerowen

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 28-August 05
  • Location: Hills of Kentucky
  • OS: Ubuntu Linux

Posted 23 October 2013 - 08:31

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)


Best Answer nitins60 , 23 October 2013 - 08:35

Windows doesn't allow ":" in file names.

Go to the full post



#2 nitins60

nitins60

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 20-January 08

Posted 23 October 2013 - 08:35   Best Answer

Windows doesn't allow ":" in file names.



#3 OP Gerowen

Gerowen

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 28-August 05
  • Location: Hills of Kentucky
  • OS: Ubuntu Linux

Posted 23 October 2013 - 08:50

Windows doesn't allow ":" in file names.

Much obliged, the issue was in the time-stamp, :p





Click here to login or here to register to remove this ad, it's free!