Python密码保护代码不写入文件

时间:2016-03-08 21:07:58

标签: python

我试图让这段代码在Python 3.5.1中运行,但我遇到的问题是没有将散列密码和用户名写入指定的文件。有什么我做错了吗?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Text-align</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
        <style>
            .cf:before,
            .cf:after {
                content: "";
                display: block;
            }
            
            .cf:after {
                clear: both;
            }
            
            body {
                margin: 0;
                font: 16px/1 sans-serif;
            }
            
            nav {
                height: 120px;
                background-color: #f2f2f2;
                padding: 0 5%;
            }
            
            ul {
                list-style: none;
                padding: 0;
                margin: 0;
            }
            
            li {
                float: left;
                color: #a6a6a6;
                border-left: 1px solid #a6a6a6;
            }
            
            li:last-child {
                border-right: 1px solid #a6a6a6;
            }
            
            li:hover {
                color: #de5728;
            }
            
            a {
                display: block;
                text-decoration: none;
                color: inherit;
                font-size: 10px;
                text-transform: uppercase;
                padding: 37px 0;
                width: 136px;
                text-align: center;
            }
            
            nav a .fa {
                display: block;
                font-size: 36px;
            }

        </style>
    </head>

    <body>
        <nav>
            <ul class="cf">
                <li>
                    <a href="#">
                        <i class="fa fa-shield"></i> Domov
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fa fa-leaf"></i> Portfolio
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fa fa-bolt"></i> O nas
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fa fa-trophy"></i> Kontakt
                    </a>
                </li>
            </ul>
        </nav>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

您没有名为passfile.txt的变量,这使得open调用失败。如果您用引号括起来,就会正确打开文件,例如'passfile.txt'

这种语法错误不明显的原因是因为你有一个全部捕获,而你的程序只会打印“写入文件时出现问题!”&#39;代替。

不是使用except:捕获所有异常,而是使用语法except IOError:,因为只要在写入文件时出现实际错误,就会抛出该语法。

答案 1 :(得分:0)

错误是你的open()函数将一个对象作为参数,但你可能想把它当作一个字符串。

此外,打开文件时通常应使用with块。

try:
    with open("passfile.txt",'a+') as f:
        f.write("{}\n{}\n".format(user_name, password))
except Exception as e: # Use other exceptions for more accurate results
    sys.exit('There was a problem writing to the file!\nError: {}'.format(str(e)))
相关问题