Sign in to follow this  
Followers 0

Python and Double Backslashes


3 posts in this topic

Posted

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)

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.