0

htpasswd ファイルにユーザー名/パスワードを作成しました。私のpythonスクリプトファイルでは、さらに処理するためにユーザー名の値にアクセスしたいと思います。どうすればそれを達成できますか?Linux OS を使用しています。

4

1 に答える 1

1

htpasswd形式は`:"で、解析はそれほど難しくありません。行ごとに調べてコロンで分割し、最初の値を取得します。

usernames = []
with open("passwdfile") as htpwd:
    for line in htpwd.readlines():
        username, pwd = line.split(":")
        usernames.append(username)

またはもっと簡潔に:

with open("passwdfile") as htpwd:
    usernames = [line.split(":")[0] for line in htpwd.readlines()]
于 2012-05-14T07:08:51.653 に答える