Basic Python Question
I am writing a .htaccess file like below: basedir = "12345678" username = "joe" dirpwd = "./htpasswd" dirpath = "/var/www/html/test/" + basedir + "/.htpasswd" filename=open(dirpath, 'w') filename.write("AuthType Basic") filename.write("AuthName Password Required") filename.write("AuthUserFile" + dirpwd) filename.write("Require user " + username) My question is how do I make each of my statements on a separate line. Once the file is created it should look like this when the .htaccess is opened: AuthType Basic AuthName "Password Required" AuthUserFile /var/www/html/test/12345678/.htpasswd Require user joe Plus, filename.write("AuthName Password Required") how do I make Password Required have quotes around it when written to the file? Thanks, Laura
Laura McCord wrote:
My question is how do I make each of my statements on a separate line. Once the file is created it should look like this when the .htaccess is opened:
Laura, You need to put \n in e.g.: filename.write("AuthType Basic\n")
Plus, filename.write("AuthName Password Required") how do I make Password Required have quotes around it when written to the file?
filename.write('AuthName "Password Required"') or filename.write("AuthName 'Password Required'") or filename.write("""AuthName "Password Required"""") -Matt -- Matt Hamilton matth@netsight.co.uk Netsight Internet Solutions, Ltd. Business Vision on the Internet http://www.netsight.co.uk +44 (0)117 9090901 Web Design | Zope/Plone Development & Consulting | Co-location | Hosting
On Wed, May 19, 2004 at 08:18:37AM -0500, Laura McCord wrote:
I am writing a .htaccess file like below:
basedir = "12345678" username = "joe" dirpwd = "./htpasswd" dirpath = "/var/www/html/test/" + basedir + "/.htpasswd" filename=open(dirpath, 'w') filename.write("AuthType Basic") filename.write("AuthName Password Required") filename.write("AuthUserFile" + dirpwd)
You're missing a space here --^
filename.write("Require user " + username)
My question is how do I make each of my statements on a separate line.
As Matt has already replied, you can add a "\n" at the end of each string. Or you can use the print statement, as it adds newlines automatically: ... f = open(dirpath, 'w') print >> f, 'AuthType Basic' print >> f, 'AuthName "Password Required"' print >> f, 'AuthUserFile ' + dirpwd print >> f, 'Require user ' + username
Once the file is created it should look like this when the .htaccess is opened:
AuthType Basic AuthName "Password Required" AuthUserFile /var/www/html/test/12345678/.htpasswd Require user joe
Plus, filename.write("AuthName Password Required") how do I make Password Required have quotes around it when written to the file?
'AuthName "Password Required"' Or "AuthName \"Password Required\"" (but it looks clumsier). HTH, Marius Gedminas -- C is for Cookies. Perl is even better for Cookies.
Marius Gedminas a écrit :
On Wed, May 19, 2004 at 08:18:37AM -0500, Laura McCord wrote:
I am writing a .htaccess file like below:
As Matt has already replied, you can add a "\n" at the end of each string. Or you can use the print statement, as it adds newlines automatically:
But os.linesep is better than '\n' as it *is* '\n' under unix, '\r\n' under windows an the good sequence ('\n\r\r'? I don't remember) under macos.
participants (4)
-
Laura McCord -
Maric MICHAUD -
Marius Gedminas -
Matt Hamilton